This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
forked from NativeScript/nativescript-dev-appium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postinstall.js
278 lines (232 loc) · 10.2 KB
/
postinstall.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env node
const {
existsSync,
mkdirSync,
readdirSync,
readFileSync,
statSync,
writeFileSync
} = require("fs");
const { basename, resolve } = require("path");
const appRootPath = require('app-root-path').toString();
const inquirer = require("inquirer");
// const chalk = require("chalk");
// const figlet = require("figlet");
const jasmine = "jasmine";
const mocha = "mocha";
const none = "none";
const js = "javascript";
const tsc = "typescript";
const ng = "angular"
const vue = "vue"
const sharedNg = "shared-ng-project"
const sharedVue = "shared-vue-project"
const projectTypes = `${tsc} | ${js} | ${ng} | ${vue} | ${sharedNg} | ${sharedVue}`;
const testingFrameworks = `${mocha} | ${jasmine} | ${none}`;
const packageJsonPath = resolve(appRootPath, "package.json");
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8"));
class Template {
constructor(testingFramwork, projectType, storage, fileExt) {
this._testingFramwork = testingFramwork;
this._projectType = projectType;
this._storage = storage;
this._fileExt = fileExt;
}
get testingFramwork() {
return this._testingFramwork;
}
get projectType() {
return this._projectType;
}
get storage() {
return this._storage;
}
get fileExt() {
return this._fileExt;
}
}
const copy = (src, dest) => {
if (!existsSync(src)) {
return Error("Source doesn't exist: " + src);
}
if (statSync(src).isDirectory()) {
if (!existsSync(dest)) {
console.log(`Create folder ${dest}`);
mkdirSync(dest);
}
const entries = new Array();
readdirSync(resolve(src)).forEach(entry => {
entries.push(entry);
});
entries.forEach(entry => {
const source = resolve(src, entry);
const destination = resolve(dest, entry);
copy(source, destination);
});
} else {
writeFileSync(dest, readFileSync(src));
}
}
const getDevDependencies = (frameworkType) => {
const testingFrameworkDeps = new Map();
testingFrameworkDeps.set(jasmine, [
{ name: "jasmine", version: "~3.3.1" },
{ name: "jasmine-core", version: "~3.3.0" },
{ name: "jasmine-spec-reporter", version: "~4.2.1" },
{ name: "@types/jasmine", version: "~3.3.4" },
{ name: "@types/node", version: "~10.12.18" },
]);
testingFrameworkDeps.set(mocha, [
{ name: "mocha", version: "~5.2.0" },
{ name: "mocha-junit-reporter", version: "~1.18.0" },
{ name: "mocha-multi", version: "~1.0.1" },
{ name: "@types/mocha", version: "~5.2.5" },
{ name: "@types/chai", version: "~4.1.7" },
{ name: "@types/node", version: "~10.12.18" },
]);
testingFrameworkDeps.set(js, []);
return testingFrameworkDeps.get(frameworkType);
}
const configureDevDependencies = (packageJson, frameworkType) => {
if (!packageJson.devDependencies) {
packageJson.devDependencies = {};
}
const devDependencies = packageJson.devDependencies || {};
getDevDependencies(frameworkType)
.filter(({ name }) => !devDependencies[name])
.forEach(({ name, version }) => {
devDependencies[name] = version;
});
}
const updatePackageJsonDependencies = (packageJson, projectType, testingFrameworkType) => {
packageJson.scripts = packageJson.scripts || {};
const checkDevDepsScript = "node ./node_modules/nativescript-dev-appium/check-dev-deps.js";
const mochaCommand = " mocha --opts ./e2e/config/mocha.opts ";
const jasmineCommand = " jasmine --config=./e2e/config/jasmine.json ";
const tscTranspile = " tsc -p e2e ";
const runner = (testingFrameworkType === none) ? undefined : (testingFrameworkType === mocha ? mochaCommand : jasmineCommand);
const executeTestsCommand = projectType !== sharedNg ? "e2e" : "e2e-appium";
const watchTestsCommandName = executeTestsCommand + "-watch";
const watchTestsCommand = "tsc -p e2e --watch";
if (!packageJson.scripts[executeTestsCommand] && runner) {
switch (projectType) {
case tsc:
case ng:
case sharedNg:
packageJson.scripts[executeTestsCommand] = checkDevDepsScript + " && " + tscTranspile + " && " + runner;
packageJson.scripts[watchTestsCommandName] = watchTestsCommand;
break;
case js:
case vue:
case sharedVue:
packageJson.scripts[executeTestsCommand] = checkDevDepsScript + " && " + runner;
break;
default:
break;
}
}
configureDevDependencies(packageJson, testingFrameworkType);
writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
}
// const printLogo = () => {
// console.log(
// chalk.green(
// figlet.textSync("{ N }-dev-appium", {
// font: "doom",
// horizontalLayout: "default",
// verticalLayout: "default"
// })
// )
// );
// };
const frameworkQuestion = () => {
const questions = [
{
type: "list",
name: "PROJECT_TYPE",
message: "What kind of project do you use?",
choices: [js, tsc, ng, vue, sharedNg]
}
];
return inquirer.prompt(questions);
};
const testingFrameworkQuestion = () => {
const questions = [
{
type: "list",
name: "TESTING_FRAMEWORK",
message: "Which testing framework do you prefer?",
choices: [mocha, jasmine, none]
}
];
return inquirer.prompt(questions);
};
const isTscProject = (PROJECT_TYPE) => { return PROJECT_TYPE === tsc || PROJECT_TYPE === ng || PROJECT_TYPE === sharedNg; }
const getTemplates = (name) => {
const templates = new Map();
templates.set("javascript.jasmine", new Template("jasmine", "javascript", "e2e-js", "js"));
templates.set("javascript.mocha", new Template("mocha", "javascript", "e2e-js", "js"));
templates.set("vue.mocha", new Template("mocha", "vue", "e2e-js", "js"));
templates.set("vue.jasmine", new Template("jasmine", "vue", "e2e-js", "js"));
templates.set("typescript.mocha", new Template("mocha", "typescript", "e2e-ts", "ts"));
templates.set("angular.mocha", new Template("mocha", "typescript", "e2e-ts", "ts"));
templates.set("typescript.jasmine", new Template("jasmine", "typescript", "e2e-ts", "ts"));
templates.set("angular.jasmine", new Template("jasmine", "typescript", "e2e-ts", "ts"));
templates.set("shared-ng-project.jasmine", new Template("jasmine", "shared-ng-project", "e2e-ts", "ts"));
return templates.get(name);
}
const run = async () => {
// printLogo();
const envProjectType = process.env.npm_config_projectType || process.env["PROJECT_TYPE"];
const envTestingFramework = process.env.npm_config_testingFramework || process.env["TESTING_FRAMEWORK"];
const hasSetProjectTypeAndTestingFrameworkAsEnvSet = envProjectType && envTestingFramework;
const isDevAppiumAlreadyInstalled = packageJson.devDependencies && packageJson.devDependencies["nativescript-dev-appium"];
const skipPostInstallOnPluginRoot = basename(appRootPath) === "nativescript-dev-appium"
if ((!hasSetProjectTypeAndTestingFrameworkAsEnvSet && isDevAppiumAlreadyInstalled) || skipPostInstallOnPluginRoot) {
console.log("Skip instalation!!!!")
return false;
}
// use env or ask questions
const { PROJECT_TYPE } = envProjectType ? { PROJECT_TYPE: envProjectType } : await frameworkQuestion();
const { TESTING_FRAMEWORK } = envTestingFramework ? { TESTING_FRAMEWORK: envTestingFramework } : await testingFrameworkQuestion();
if (!projectTypes.includes(PROJECT_TYPE)) {
console.error(`Please provide PROJECT_TYPE of type ${projectTypes}!`);
return;
}
if (!testingFrameworks.includes(TESTING_FRAMEWORK)) {
console.error(`Please provide testingFramework of type ${testingFrameworks}!`);
return;
}
const sampleTestsProjectFolderPath = resolve(appRootPath, "e2e");
const basicSampleTestsPluginFolderPath = resolve(appRootPath, "node_modules", "nativescript-dev-appium", "samples");
if (!existsSync(sampleTestsProjectFolderPath) && TESTING_FRAMEWORK !== none) {
mkdirSync(sampleTestsProjectFolderPath);
const template = getTemplates(`${PROJECT_TYPE}.${TESTING_FRAMEWORK}`);
const e2eSamplesFolder = resolve(basicSampleTestsPluginFolderPath, template.storage);
if (isTscProject(template.projectType)) {
const tsConfigJsonFile = resolve(e2eSamplesFolder, "tsconfig.json");
const tsConfigJson = JSON.parse(readFileSync(tsConfigJsonFile, "utf8"));
switch (template.testingFramwork) {
case jasmine:
tsConfigJson.compilerOptions.types.push("jasmine");
break;
case mocha:
tsConfigJson.compilerOptions.types.push("mocha");
tsConfigJson.compilerOptions.types.push("chai");
break;
default:
break;
}
writeFileSync(tsConfigJsonFile, JSON.stringify(tsConfigJson, null, 2));
copy(tsConfigJsonFile, resolve(sampleTestsProjectFolderPath, "tsconfig.json"));
}
const samplesFilePostfix = "sample.e2e-spec";
copy(resolve(e2eSamplesFolder, `${template.projectType}.${template.testingFramwork}.${samplesFilePostfix}.${template.fileExt}`), resolve(sampleTestsProjectFolderPath, `${samplesFilePostfix}.${template.fileExt}`));
copy(resolve(e2eSamplesFolder, `${template.testingFramwork}.setup.${template.fileExt}`), resolve(sampleTestsProjectFolderPath, `setup.${template.fileExt}`));
copy(resolve(basicSampleTestsPluginFolderPath, "config"), resolve(sampleTestsProjectFolderPath, "config"));
const settingsFile = template.testingFramwork === jasmine ? `${template.testingFramwork}.json` : `${template.testingFramwork}.opts`;
copy(resolve(basicSampleTestsPluginFolderPath, settingsFile), resolve(sampleTestsProjectFolderPath, "config", settingsFile));
}
updatePackageJsonDependencies(packageJson, PROJECT_TYPE, TESTING_FRAMEWORK);
};
run();