forked from getgauge/gauge-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launcher.js
executable file
·172 lines (146 loc) · 4.98 KB
/
launcher.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
#! /usr/bin/env node
var tsVersion = require("./ts.json").version;
var version = process.versions.node.split(".");
if (parseInt(version[0]) < 10) {
throw new Error("gauge-ts requires Node.js version 10+. Current version: " + process.versions.node);
}
let stepImpl = `
import { Step, Table } from "gauge-ts";
import { strictEqual } from "assert";
export default class StepImplementation {
private vowels: Array<string>;
@Step("Vowels in English language are <vowelString>.")
public async setLanguageVowels(vowelString: string) {
this.vowels = vowelString.split('');
}
@Step("The word <word> has <expectedCount> vowels.")
public async verifyVowelsCountInWord(word: string, expectedCount: number) {
strictEqual(this.countVowels(word), parseInt(expectedCount));
}
@Step("Almost all words have vowels <wordsTable>")
public async verifyVowelsCountInMultipleWords(table: Table) {
for (let row of table.getTableRows()) {
let word: string = row.getCell("Word");
let expectedCount = parseInt(row.getCell("Vowel Count"));
let actualCount = this.countVowels(word);
strictEqual(expectedCount, actualCount);
}
}
private async countVowels(word: string) {
return word.split("").filter((elem) => {
return this.vowels.includes(elem);
}).length;
}
}
`
let defaultProperties = `
#ts.properties
#settings related to gauge-ts.
# Comma separated list of dirs. path should be relative to project root.
STEP_IMPL_DIR = tests
`
let packageJson = `
{
"name": "gauge-ts-template",
"version": "0.0.1",
"description": "Starter template for writing TypeScript tests for Gauge",
"dependencies": {
"gauge-ts": "${tsVersion}"
},
"devDependencies": {
"@types/node": "latest"
}
}
`
let tsconfig = `
{
"compilerOptions": {
/* Basic Options */
"target": "es6",
/* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
"module": "commonjs",
/* Specify module code generation: 'none', commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": ["es2016"], /* Specify library files to be included in the compilation: */
/* Module Resolution Options */
"moduleResolution": "node",
/* Experimental Options */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
"emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
},
"exclude": [
"node_modules",
]
}
`
let fs = require("fs");
let path = require("path");
let cp = require("child_process");
let os = require('os');
const { GAUGE_PROJECT_ROOT } = process.env;
let testsDir = path.join(GAUGE_PROJECT_ROOT, 'tests');
let envDir = path.join(GAUGE_PROJECT_ROOT, 'env');
let packageJsonFile = path.join(GAUGE_PROJECT_ROOT, 'package.json');
let tsconfigFile = path.join(GAUGE_PROJECT_ROOT, 'tsconfig.json');
function getCommand(command) {
let validExecExt = [""];
if (os.platform() === 'win32') validExecExt.push(".bat", ".exe", ".cmd");
for (const ext of validExecExt) {
let executable = `${command}${ext}`;
if (!cp.spawnSync(executable).error) return executable;
}
}
if (process.argv[2] === "--init") {
console.log("Initializing Gauge TypeScript project");
fs.mkdir(testsDir, 484, function (err) {
if (err && err.code !== "EEXIST") {
console.error(err);
} else {
fs.writeFileSync(path.join(testsDir, 'StepImplementation.ts'), stepImpl);
}
});
fs.mkdir(envDir, 484, function (err) {
if (err && err.code !== "EEXIST") {
console.error(err);
} else {
let defaultDir = path.join(envDir, 'default');
fs.mkdir(defaultDir, 484, function (err) {
if (err && err.code !== "EEXIST") {
console.error(err);
} else {
fs.writeFileSync(path.join(defaultDir, 'ts.properties'), defaultProperties);
}
});
}
});
fs.writeFileSync(packageJsonFile, packageJson);
fs.writeFileSync(tsconfigFile, tsconfig);
console.log("Run `npm install` to get project dependencies.");
}
else if (process.argv[2] === "--start") {
var script = 'import { GaugeRuntime } from "gauge-ts/dist/GaugeRuntime";'
+ `let runner = new GaugeRuntime();`
+ `runner.start();`
var opts = [
'-O', `{"experimentalDecorators": true,"emitDecoratorMetadata": true}`,
...hasModule('tsconfig-paths') ? ['-r', 'tsconfig-paths/register'] : [],
'-e', script
];
let tsNode = path.join(GAUGE_PROJECT_ROOT, 'node_modules', '.bin', 'ts-node');
var runner = cp.spawn(getCommand(tsNode), opts, {
env: process.env,
silent: false,
stdio: "inherit",
cwd: GAUGE_PROJECT_ROOT
});
runner.on("error", function (err) {
console.trace(err.stack);
});
}
function hasModule(name) {
try {
require.resolve(name, { paths: [GAUGE_PROJECT_ROOT] });
return true;
} catch(e) {
return false;
}
}