Skip to content

Commit

Permalink
Update dependencies and make it works
Browse files Browse the repository at this point in the history
  • Loading branch information
3mp3ri0r committed Mar 26, 2022
1 parent 326f322 commit f83a619
Show file tree
Hide file tree
Showing 38 changed files with 2,200 additions and 1,573 deletions.
48 changes: 31 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,49 @@
},
"bugs": "https://github.com/foambubble/foam/issues",
"dependencies": {
"@oclif/command": "^1",
"@oclif/config": "^1",
"@oclif/plugin-help": "^3",
"foam-core": "^0.11.0",
"@oclif/command": "^1.8.16",
"@oclif/config": "^1.18.3",
"@oclif/plugin-help": "^5.1.12",
"detect-newline": "^3.1.0",
"fast-array-diff": "^1.0.1",
"github-slugger": "^1.4.0",
"glob": "^7.1.6",
"lodash": "^4.17.21",
"micromatch": "^4.0.2",
"ora": "^4.0.4",
"tslib": "^1"
"remark-frontmatter": "^2.0.0",
"remark-parse": "^8.0.2",
"remark-wiki-link": "^0.0.4",
"title-case": "^3.0.2",
"unified": "^9.0.0",
"unist-util-visit": "^2.0.2",
"yaml": "^1.10.0"
},
"devDependencies": {
"@babel/core": "^7.10.4",
"@babel/preset-env": "^7.10.4",
"@babel/preset-typescript": "^7.10.4",
"@oclif/dev-cli": "^1",
"@types/node": "^10",
"babel-jest": "^26.1.0",
"@babel/core": "^7.17.8",
"@babel/preset-env": "^7.16.11",
"@babel/preset-typescript": "^7.16.7",
"@oclif/dev-cli": "^1.26.10",
"@types/github-slugger": "^1.3.0",
"@types/glob": "^7.1.1",
"@types/jest": "^27.4.1",
"@types/lodash": "^4.14.157",
"@types/micromatch": "^4.0.1",
"@types/node": "^13.11.0",
"babel-jest": "^27.5.1",
"chai": "^4",
"eslint": "^5.13",
"eslint-config-oclif": "^3.1",
"eslint-config-oclif-typescript": "^0.1",
"globby": "^10",
"jest": "^26.1.0",
"mock-fs": "^4.12.0",
"ts-node": "^8",
"ts-node": "^10.7.0",
"tslib": "^2.3.1",
"typescript": "^3.3"
},
"peerDependencies": {
"foam-core": "*"
},
"engines": {
"node": ">=12.0.0"
"node": ">=16.0.0"
},
"files": [
"/bin",
Expand All @@ -56,7 +70,7 @@
"@oclif/plugin-help"
]
},
"repository": "foambubble/foam",
"repository": "foambubble/foam-cli",
"scripts": {
"clean": "rimraf tmp",
"build": "tsc -b",
Expand Down
29 changes: 17 additions & 12 deletions src/commands/janitor.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { Command, flags } from '@oclif/command';
import ora from 'ora';
import {
bootstrap,
createConfigFromFolders,
generateLinkReferences,
generateHeading,
applyTextEdit,
FileDataStore,
URI,
isNote,
} from 'foam-core';
import { MarkdownResourceProvider } from "../core/markdown-provider";
import { bootstrap } from '../core/model/foam';
import { URI } from '../core/model/uri';
import { Resource } from '../core/model/note';
import { generateHeading, generateLinkReferences } from '../core/janitor';
import { applyTextEdit } from '../core/janitor/apply-text-edit';
import { FileDataStore, Matcher } from '../core/services/datastore';
import { writeFileToDisk } from '../utils/write-file-to-disk';
import { isValidDirectory } from '../utils';

const isNote = (resource: Resource): resource is Resource => resource.type === 'note'

export default class Janitor extends Command {
static description =
'Updates link references and heading across all the markdown files in the given workspaces';
Expand Down Expand Up @@ -41,8 +40,14 @@ export default class Janitor extends Command {
const { workspacePath = './' } = args;

if (isValidDirectory(workspacePath)) {
const config = createConfigFromFolders([URI.file(workspacePath)]);
const workspace = (await bootstrap(config, { dataStore: new FileDataStore(config)}))
const matcher = new Matcher(
[URI.file(workspacePath)],
['**/*'],
[]
);
const dataStore = new FileDataStore();
const markdownProvider = new MarkdownResourceProvider(matcher);
const workspace = (await bootstrap(matcher, dataStore, [markdownProvider]))
.workspace;

const notes = workspace.list().filter(isNote);
Expand Down
39 changes: 25 additions & 14 deletions src/commands/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import GithubSlugger from 'github-slugger';
import { Command, flags } from '@oclif/command';
import ora from 'ora';
import {
bootstrap,
createConfigFromFolders,
generateLinkReferences,
generateHeading,
getKebabCaseFileName,
applyTextEdit,
FileDataStore,
isNote,
} from 'foam-core';
import { bootstrap } from '../core/model/foam';
import { URI } from '../core/model/uri';
import { Resource } from '../core/model/note';
import { generateHeading, generateLinkReferences } from '../core/janitor';
import { applyTextEdit } from '../core/janitor/apply-text-edit';
import { FileDataStore, Matcher } from '../core/services/datastore';
import { writeFileToDisk } from '../utils/write-file-to-disk';
import { renameFile } from '../utils/rename-file';
import { isValidDirectory } from '../utils';

const slugger = new GithubSlugger();

const isNote = (resource: Resource): resource is Resource => resource.type === 'note'

const getKebabCaseFileName = (fileName: string) => {
const kebabCasedFileName = slugger.slug(fileName);
return kebabCasedFileName === fileName ? null : kebabCasedFileName;
};

// @todo: Refactor 'migrate' and 'janitor' commands and avoid repeatition
export default class Migrate extends Command {
static description =
Expand Down Expand Up @@ -42,11 +48,16 @@ Successfully generated link references and heading!
const { args, flags } = this.parse(Migrate);

const { workspacePath = './' } = args;
const config = createConfigFromFolders([workspacePath]);

const matcher = new Matcher(
[URI.file(workspacePath)],
['**/*'],
[]
);
const dataStore = new FileDataStore();

if (isValidDirectory(workspacePath)) {
const dataStore = new FileDataStore(config);
let workspace = (await bootstrap(config, { dataStore: dataStore})).workspace;
let workspace = (await bootstrap(matcher, dataStore, [])).workspace;

let notes = workspace.list().filter(isNote);

Expand Down Expand Up @@ -75,7 +86,7 @@ Successfully generated link references and heading!
spinner.text = 'Renaming files';

// Reinitialize the graph after renaming files
workspace = (await bootstrap(config, { dataStore: dataStore})).workspace;
workspace = (await bootstrap(matcher, dataStore, [])).workspace;

notes = workspace.list().filter(isNote);

Expand Down
2 changes: 1 addition & 1 deletion src/core/common/snippetParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ export class TextmateSnippet extends Marker {
resolvedVariables.forEach(variable => {
result += this.value.substring(i, variable.pos);
result += variable.toString();
i = variable.endPos;
i = variable.endPos ?? i;
});
result += this.value.substring(i);

Expand Down
26 changes: 13 additions & 13 deletions src/core/janitor/generateLinkReferences.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ describe('generateLinkReferences', () => {

const actual = generateLinkReferences(note, _workspace, false);

expect(actual!.range.start).toEqual(expected.range.start);
expect(actual!.range.end).toEqual(expected.range.end);
expect(actual!.newText).toEqual(expected.newText);
expect(actual?.range.start).toEqual(expected.range.start);
expect(actual?.range.end).toEqual(expected.range.end);
expect(actual?.newText).toEqual(expected.newText);
});

it('should remove link definitions from a file that has them, if no links are present', () => {
Expand All @@ -62,9 +62,9 @@ describe('generateLinkReferences', () => {

const actual = generateLinkReferences(note, _workspace, false);

expect(actual!.range.start).toEqual(expected.range.start);
expect(actual!.range.end).toEqual(expected.range.end);
expect(actual!.newText).toEqual(expected.newText);
expect(actual?.range.start).toEqual(expected.range.start);
expect(actual?.range.end).toEqual(expected.range.end);
expect(actual?.newText).toEqual(expected.newText);
});

it('should update link definitions if they are present but changed', () => {
Expand All @@ -82,9 +82,9 @@ describe('generateLinkReferences', () => {

const actual = generateLinkReferences(note, _workspace, false);

expect(actual!.range.start).toEqual(expected.range.start);
expect(actual!.range.end).toEqual(expected.range.end);
expect(actual!.newText).toEqual(expected.newText);
expect(actual?.range.start).toEqual(expected.range.start);
expect(actual?.range.end).toEqual(expected.range.end);
expect(actual?.newText).toEqual(expected.newText);
});

it('should not cause any changes if link reference definitions were up to date', () => {
Expand Down Expand Up @@ -112,9 +112,9 @@ describe('generateLinkReferences', () => {

const actual = generateLinkReferences(note, _workspace, false);

expect(actual!.range.start).toEqual(expected.range.start);
expect(actual!.range.end).toEqual(expected.range.end);
expect(actual!.newText).toEqual(expected.newText);
expect(actual?.range.start).toEqual(expected.range.start);
expect(actual?.range.end).toEqual(expected.range.end);
expect(actual?.newText).toEqual(expected.newText);
});

it('should not remove explicitly entered link references', () => {
Expand Down Expand Up @@ -155,5 +155,5 @@ describe('generateLinkReferences', () => {
* @param text starting text, using a \n line separator
*/
function textForNote(note: Resource, text: string): string {
return text.split('\n').join(note.source.eol);
return text.split('\n').join(note?.source.eol);
}
32 changes: 18 additions & 14 deletions src/core/markdown-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,12 @@ const sectionsPlugin: ParserPlugin = {
sectionStack[sectionStack.length - 1].level >= level
) {
const section = sectionStack.pop();
note.sections.push({
label: section.label,
range: Range.createFromPosition(section.start, start),
});
if (section) {
note.sections.push({
label: section.label,
range: Range.createFromPosition(section.start, start),
});
}
}

// Add the new section to the stack
Expand All @@ -254,10 +256,12 @@ const sectionsPlugin: ParserPlugin = {
// Close all the remainig sections
while (sectionStack.length > 0) {
const section = sectionStack.pop();
note.sections.push({
label: section.label,
range: { start: section.start, end },
});
if (section) {
note.sections.push({
label: section.label,
range: { start: section.start, end },
});
}
}
note.sections.sort((a, b) =>
Position.compareTo(a.range.start, b.range.start)
Expand Down Expand Up @@ -386,7 +390,7 @@ export function createMarkdownParser(
try {
plugin.onDidInitializeParser?.(parser);
} catch (e) {
handleError(plugin, 'onDidInitializeParser', undefined, e);
handleError(plugin, 'onDidInitializeParser', undefined, e as Error);
}
});

Expand All @@ -397,7 +401,7 @@ export function createMarkdownParser(
try {
return plugin.onWillParseMarkdown?.(acc) || acc;
} catch (e) {
handleError(plugin, 'onWillParseMarkdown', uri, e);
handleError(plugin, 'onWillParseMarkdown', uri, e as Error);
return acc;
}
}, markdown);
Expand Down Expand Up @@ -425,7 +429,7 @@ export function createMarkdownParser(
try {
plugin.onWillVisitTree?.(tree, note);
} catch (e) {
handleError(plugin, 'onWillVisitTree', uri, e);
handleError(plugin, 'onWillVisitTree', uri, e as Error);
}
});
visit(tree, node => {
Expand All @@ -446,7 +450,7 @@ export function createMarkdownParser(
try {
plugins[i].onDidFindProperties?.(yamlProperties, note, node);
} catch (e) {
handleError(plugins[i], 'onDidFindProperties', uri, e);
handleError(plugins[i], 'onDidFindProperties', uri, e as Error);
}
}
} catch (e) {
Expand All @@ -458,15 +462,15 @@ export function createMarkdownParser(
try {
plugins[i].visit?.(node, note, markdown);
} catch (e) {
handleError(plugins[i], 'visit', uri, e);
handleError(plugins[i], 'visit', uri, e as Error);
}
}
});
plugins.forEach(plugin => {
try {
plugin.onDidVisitTree?.(tree, note);
} catch (e) {
handleError(plugin, 'onDidVisitTree', uri, e);
handleError(plugin, 'onDidVisitTree', uri, e as Error);
}
});
Logger.debug('Result:', note);
Expand Down
5 changes: 3 additions & 2 deletions src/core/model/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ export class FoamGraph implements IDisposable {
const resourcesToUpdate: URI[] = [];
for (const placeholderId of this.placeholders.keys()) {
// quick and dirty check for affected resources
if (resource.uri.path.endsWith(placeholderId + '.md')) {
const backlink = this.backlinks.get(placeholderId)
if (resource.uri.path.endsWith(placeholderId + '.md') && backlink) {
resourcesToUpdate.push(
...this.backlinks.get(placeholderId).map(c => c.source)
...backlink.map(c => c.source)
);
// resourcesToUpdate.push(resource);
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/model/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('Workspace resources', () => {
['note2', '/note2.md'],
]) {
expect(ws.listByIdentifier(reference)[0].uri.path).toEqual(path);
expect(ws.find(reference).uri.path).toEqual(path);
expect(ws.find(reference)?.uri.path).toEqual(path);
}
});

Expand All @@ -86,7 +86,7 @@ describe('Workspace resources', () => {
.set(createTestNote({ uri: 'file.md' }));

const res = ws.find('test-file#my-section');
expect(res.uri.fragment).toEqual('my-section');
expect(res?.uri.fragment).toEqual('my-section');
});
});

Expand Down
Loading

0 comments on commit f83a619

Please sign in to comment.