Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat #4 #6

Merged
merged 3 commits into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 9 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,19 @@

const { TextEdit, Position, Command } = require("vscode-languageserver");
const { URI } = require("vscode-uri");

function normalizeToAngleBracketComponent(name) {
const SIMPLE_DASHERIZE_REGEXP = /[a-z]|\/|-/g;
const ALPHA = /[A-Za-z0-9]/;

if (name.includes(".")) {
return name;
}

return name.replace(SIMPLE_DASHERIZE_REGEXP, (char, index) => {
if (char === "/") {
return "::";
}

if (index === 0 || !ALPHA.test(name[index - 1])) {
return char.toUpperCase();
}

// Remove all occurrences of '-'s from the name that aren't starting with `-`
return char === "-" ? "" : char.toLowerCase();
});
}
const {
normalizeToAngleBracketComponent,
waitForFileNameContains,
watcherFn,
} = require("./utils");

module.exports = {
onInit(_, project) {
if (!("els.executeInEmberCLI" in project.executors)) {
console.error('Unable to find "ember-fast-cli" addon.');
return;
}
project.addWatcher(watcherFn);
project.executors["els.extractSourceCodeToComponent"] = async (
server,
filePath,
Expand All @@ -43,13 +27,13 @@ module.exports = {
try {
// const ast = server.templateCompletionProvider.getAST(document.getText(range));
// const focusPath = server.templateCompletionProvider.createFocusPath(ast, ast.loc.start, text);
const componentName = rawComponentName.trim().split(" ").pop();
const waiter = waitForFileNameContains(componentName);
await server.onExecute({
command: "els.executeInEmberCLI",
arguments: [filePath, `g component ${rawComponentName}`],
});
const componentName = rawComponentName.trim().split(" ").pop();
// going to wait for file changes api
await new Promise((resolve) => setTimeout(resolve, 2000));
await waiter;
const registry = server.getRegistry(project.root);
if (!(componentName in registry.component)) {
console.log(
Expand Down
71 changes: 71 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
function normalizeToAngleBracketComponent(name) {
const SIMPLE_DASHERIZE_REGEXP = /[a-z]|\/|-/g;
const ALPHA = /[A-Za-z0-9]/;

if (name.includes(".")) {
return name;
}

return name.replace(SIMPLE_DASHERIZE_REGEXP, (char, index) => {
if (char === "/") {
return "::";
}

if (index === 0 || !ALPHA.test(name[index - 1])) {
return char.toUpperCase();
}

// Remove all occurrences of '-'s from the name that aren't starting with `-`
return char === "-" ? "" : char.toLowerCase();
});
}

let matchFunctions = [];

function normalizeFilePath(filePath) {
return filePath.split('\\').join('/');
}

function hasMatchFunctions() {
return matchFunctions.length > 0;
}

function waitForFileNameContains(name, timeout = 2000) {
let timeoutUid = null;
let resolve = null;
let reject = null;
let deleteFunction = null;
let item = new Promise((res, rej) => {
resolve = res;
reject = () => {
deleteFunction();
rej();
};
});
timeoutUid = setTimeout(reject, timeout);
let fn = (uri) => {
if (normalizeFilePath(uri).includes(name)) {
deleteFunction();
setTimeout(resolve);
}
};
matchFunctions.push(fn);
deleteFunction = () => {
clearTimeout(timeoutUid);
matchFunctions = matchFunctions.filter((f) => f !== fn);
};
return item;
}

function watcherFn(uri, changeType) {
// created
if (changeType !== 2) {
return;
}
matchFunctions.forEach((fn) => fn(uri));
}

module.exports.normalizeToAngleBracketComponent = normalizeToAngleBracketComponent;
module.exports.watcherFn = watcherFn;
module.exports.waitForFileNameContains = waitForFileNameContains;
module.exports.hasMatchFunctions = hasMatchFunctions;
68 changes: 68 additions & 0 deletions utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const {
normalizeToAngleBracketComponent,
waitForFileNameContains,
watcherFn,
hasMatchFunctions,
} = require("./utils");

describe("normalizeToAngleBracketComponent", () => {
it("ok", () => {
expect(normalizeToAngleBracketComponent("foo-bar")).toBe("FooBar");
expect(normalizeToAngleBracketComponent("foo-bar/baz-boo")).toBe(
"FooBar::BazBoo"
);
expect(normalizeToAngleBracketComponent("foo.boo")).toBe("foo.boo");
});
});

describe("waitForFileNameContains", () => {
it("ok", async () => {
try {
const waiter = waitForFileNameContains("foo", 100);
expect(hasMatchFunctions()).toBe(true);
watcherFn("fos", 2);
expect(hasMatchFunctions()).toBe(true);
watcherFn("foo", 2);
expect(hasMatchFunctions()).toBe(false);
await waiter;
} catch (e) {
expect(e.toString()).toBe(null);
}
});
it("supports win paths", async () => {
try {
const waiter = waitForFileNameContains("foo/bar", 100);
expect(hasMatchFunctions()).toBe(true);
watcherFn("foo\\bar", 2);
expect(hasMatchFunctions()).toBe(false);
await waiter;
} catch (e) {
expect(e.toString()).toBe(null);
}
});
it("failing", async () => {
try {
const waiter = waitForFileNameContains("foo", 100);
watcherFn("foz", 1);
expect(hasMatchFunctions()).toBe(true);
await waiter;
} catch (e) {
expect(hasMatchFunctions()).toBe(false);
}
});
it("removing", async () => {
const waiter = waitForFileNameContains("foo", 100);
expect(hasMatchFunctions()).toBe(true);
watcherFn("foo", 2);
expect(hasMatchFunctions()).toBe(false);
await waiter;
});
it("has default timeout", async () => {
const waiter = waitForFileNameContains("foo");
expect(hasMatchFunctions()).toBe(true);
watcherFn("foo", 2);
expect(hasMatchFunctions()).toBe(false);
await waiter;
expect(1).toBe(1);
});
});