-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
155 lines (139 loc) · 4.01 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
#!/usr/bin/env node
import { promises as fs } from 'fs';
import path from 'path';
import { exit } from 'process';
import { fileURLToPath } from 'url';
import yargsParser from 'yargs-parser';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import createVueComponentTests from './src/createVueComponentTests.js';
import createVuexStoreTests from './src/createVuexStoreTests.js';
export default async function createUnitTest(argPath, args) {
const targetPath = path.resolve(argPath);
const pathParsed = path.parse(targetPath);
const parentPath = path.resolve(targetPath, '..');
if (!pathParsed.ext) {
const stat = await fs.lstat(targetPath);
if (stat.isDirectory()) {
let filesInDir = await fs.readdir(targetPath);
/**
* create store tests from store /storename/*.js files
*/
const storeFiles = ['actions.js', 'getters.js', 'mutations.js'];
const areFiles = filesInDir.some((file) => storeFiles.includes(file));
if (areFiles) {
for (const file of filesInDir) {
if (!storeFiles.includes(file)) {
continue;
}
const fileName = file.split('.')[0];
await createVuexStoreTests(
fileName,
pathParsed.name,
targetPath,
args,
);
}
return;
}
/**
* create store tests from /storename/index.js file
*/
if (filesInDir.includes('index.js')) {
return await createVuexStoreTests(
'index',
pathParsed.name,
targetPath,
args,
);
}
}
}
if (pathParsed.ext === '.js') {
/**
* create store tests from /storename/*.js file
*/
if (
['actions', 'getters', 'mutations', 'index'].includes(pathParsed.name)
) {
return await createVuexStoreTests(
pathParsed.name,
path.parse(parentPath).name,
parentPath,
args,
);
}
/**
* create store tests from storename.js file
*/
return await createVuexStoreTests(
pathParsed.name,
pathParsed.name,
parentPath,
args,
);
}
if (pathParsed.ext !== '.vue') {
console.error(`
Incorect file extension.
Supported [.vue] for coponents and [.js] for store
`);
}
/**
* create component tests from component.vue file
*/
if (pathParsed.ext === '.vue') {
return await createVueComponentTests(
targetPath,
pathParsed.name,
parentPath,
args,
);
}
}
/**
* cli
*/
const scriptArgs = yargsParser(process.argv.slice(2));
if (scriptArgs['help'] || scriptArgs['h'] || process.argv.length <= 2) {
console.log(
'\x1b[33m%s\x1b[0m',
`
Unit tests generator for Vue components
-h --help # show this message and quit
-n --name # set custom name for spec file
-d --testDir # relative directory where spec file will be placed, default "__tests__"
-a --addTests # will add initial tests schemas in "describe()" section
-v --verbose # print data passed to template
--clip # copy code and generated test to clipboard
--version # print version
--template # set path to custom hygen.io template
--dry # dry run
`,
);
exit();
}
if (scriptArgs['version']) {
async function printPackageVersion() {
const packageJson = await fs.readFile(
path.resolve(__dirname, 'package.json'),
'utf-8',
);
const { version } = JSON.parse(packageJson);
console.log(version);
exit();
}
printPackageVersion();
}
if (scriptArgs && scriptArgs['_'].length && !scriptArgs['version']) {
scriptArgs['_'].forEach((filePath) => {
try {
createUnitTest(path.normalize(filePath), {
rootDir: __dirname,
...scriptArgs,
});
} catch (err) {
console.error(err);
}
});
}