generated from lifeart/els-a11y-addon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
161 lines (154 loc) · 4.73 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"use strict";
const { TextEdit, Position, Range, Command } = require("vscode-languageserver");
const fs = require("fs");
const { URI } = require("vscode-uri");
const {
normalizeToAngleBracketComponent,
waitForFileNameContains,
watcherFn,
} = require("./utils");
const { transformSelection } = require("./transformers");
const { transformTests } = require("./testing-transformers");
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,
[rawComponentName, { range, source, uri }]
) => {
if (!rawComponentName.trim()) {
console.log("no component name found");
return;
}
try {
let result = {
code: source,
shape: {},
args: [],
};
let rootRegistry = server.getRegistry(project.root);
try {
const helpers = Object.keys(rootRegistry["helper"]);
const components = Object.keys(rootRegistry["component"]);
const modifiers = Object.keys(rootRegistry["modifier"]);
result = transformSelection(
source,
[].concat(helpers, components, modifiers)
);
} catch (e) {
console.log(e.toString());
}
let { code, args, shape } = result;
let argNames = args
.slice(0)
.map((el) => el.split("=")[0].replace("@", ""));
if (args.length) {
args = " " + args.join("\n ");
args = ` \n${args} \n`;
} else {
args = "";
}
const componentName = rawComponentName.trim().split(" ").pop();
const tagName = normalizeToAngleBracketComponent(componentName);
const waiter = waitForFileNameContains(componentName);
await server.onExecute({
command: "els.executeInEmberCLI",
arguments: [filePath, `g component ${rawComponentName}`],
});
try {
await waiter;
} catch (e) {
console.log("unable to find document change event");
}
const registry = server.getRegistry(project.root);
if (!(componentName in registry.component)) {
console.log(
`Unable to find component ${componentName} in registry ${JSON.stringify(
Object.keys(registry.component)
)}`
);
return;
}
const componentRegistry = registry["component"][componentName];
const fileName = componentRegistry.find((file) =>
file.endsWith(".hbs")
);
const testFileName = componentRegistry.find(
(file) => file.includes("test") && file.endsWith(".js")
);
if (!fileName) {
console.log(
`Unable to find template file for component ${componentName}`
);
return;
}
const fileUri = URI.file(fileName).toString();
const edit = {
changes: {
[uri]: [TextEdit.replace(range, `<${tagName} ${args}/>`)],
[fileUri]: [
TextEdit.replace(
Range.create(
Position.create(0, 0),
Position.create(0, code.length)
),
code
),
],
},
};
if (testFileName) {
try {
const testContent = fs.readFileSync(testFileName, "utf8");
const newTestContent = transformTests(
testContent,
tagName,
argNames, shape
);
edit.changes[URI.file(testFileName).toString()] = [
TextEdit.replace(
Range.create(
Position.create(0, 0),
Position.create(
testContent.split("\n").length,
testContent.length
)
),
newTestContent
),
];
} catch (e) {
console.log(e.toString());
}
}
await server.connection.workspace.applyEdit(edit);
} catch (e) {
console.log(e.toString());
}
};
},
onCodeAction(_, params) {
if (!params.textDocument.uri.endsWith(".hbs")) {
return;
}
const act = Command.create(
"Extract selection to component",
"els.getUserInput",
{
placeHolder: "Enter component name",
},
"els.extractSourceCodeToComponent",
{
source: params.document.getText(params.range),
range: params.range,
uri: params.textDocument.uri,
}
);
return [act];
},
};