-
Notifications
You must be signed in to change notification settings - Fork 58
/
apprun-cli.js
executable file
·102 lines (81 loc) · 2.67 KB
/
apprun-cli.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
#!/usr/bin/env node
const { program } = require('commander');
const { existsSync, writeFileSync, mkdirSync } = require('fs');
const { resolve, dirname } = require('path');
const { red, green, yellow } = require('./cli/colors');
const component_template = `import {app, Component} from 'apprun';
export default class #nameComponent extends Component {
state = '#name';
view = (state) => <>
<h1>{state}</h1>
</>;
// update = {
// '/#name': state => state,
// }
}
`;
const test_template = `import app from 'apprun';
import #name from './#fn';
describe('component', () => {
it('should render state upon route event', () => {
const element = document.createElement('div');
const component = new #name().mount(element);
//app.run('/#name');
component.run('.');
expect(element.textContent).toBe('#name');
})
})
`;
program
.version('3.0.0')
.description('AppRun CLI')
.option('-i, --init', 'Initialize AppRun Project')
.option('-c, --component <name>', 'Create a component')
.option('-t, --test <name>', 'Create a component spec')
.option('-p, --pages [directory]', 'Create example pages')
program.parse(process.argv);
const options = program.opts();
if (options.init) {
console.log(`\nPlease use ${yellow('npm create apprun-app')} to create AppRun projects.\n`);
return;
}
function createTestFile(fn, name) {
const component_name = name || fn.split('/').pop();
const component_file = fn.split('/').pop();
const test = test_template
.replace(/#name/g, component_name)
.replace(/#fn/g, component_file);
writeFile(fn + '.spec.tsx', test);
}
function createComponent(fn, name) {
if (!name) name = fn.split('/').pop();
const component = component_template.replace(/#name/g, name);
writeFile(fn + '.tsx', component);
}
const cwd = process.cwd();
const writeFile = (fn, text) => {
fn = resolve(cwd, fn);
const dir = dirname(fn);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
console.log(yellow(`✔ ${dir}`));
}
if (existsSync(fn)) {
console.log(red(`✘ ${fn} exists`));
return;
}
writeFileSync(fn, text, 'utf8');
console.log(green(`✔ ${fn}`));
}
options.component && createComponent(options.component);
options.test && createTestFile(options.test);
if (options.pages) {
let pages = typeof options.pages === 'string' ? options.pages : 'pages';
pages = resolve(cwd, pages);
createComponent(`${pages}/Home/index`, 'Home');
createTestFile(`${pages}/Home/index`, 'Home');
createComponent(`${pages}/About/index`, 'About');
createTestFile(`${pages}/About/index`, 'About');
createComponent(`${pages}/Contact/index`, 'Contact');
createTestFile(`${pages}/Contact/index`, 'Contact');
}