-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·175 lines (149 loc) · 5.92 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env node
const fs = require('fs');
const fse = require('fs-extra');
const path = require('path');
const webpack = require('webpack');
const program = require('commander');
const util = require('util');
const createBundleRenderer = require('vue-server-renderer')
.createBundleRenderer;
const colors = require('colors');
const isValid = require('is-valid-path');
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const copyFile = util.promisify(fs.copyFile);
const ensureFile = util.promisify(fse.ensureFile);
const package = require('./package.json');
const webpackConfig = require('./lib/webpack.skeleton.conf');
const config = require('./config');
program
.version('1.0.0', '-v, --version')
.description('Tool of skeleton')
.command('generate')
.alias('g')
.description('generate the skeleton template')
.option('-s, --source <source>', 'assign the source of skeleton entry')
.option('-t, --target <target>', 'assign the path of output skeleton html')
.action(async function(options) {
if (!isValid(options.source)) {
console.error(colors.red(`option 'source': ${options.source} is not a path, eg: ./xxx.vue, please verify!`));
throw new Error('options.source is not a path');
}
if (!isValid(options.target)) {
console.error(colors.red(`option 'target': ${options.target} is not a path, eg: ./xxx.html, please verify!`));
throw new Error('options.target is not a path');
}
const processPath = process.cwd();
const sourceAbsolutePath = path.resolve(processPath, options.source);
const targetAbsolutePath = path.resolve(processPath, options.target);
const cliAbsolutePath = __dirname;
const skeletonEntryAbsolutePath = require.resolve(
'./lib/skeleton.entry.js'
);
const SKELETON_ENTRY_RELATIVE_PATH = path
.relative(skeletonEntryAbsolutePath, sourceAbsolutePath)
.substr('../'.length);
if (!config.copySkeletonTemplate) {
const skeletonEntryTempalte = await readFile(
require.resolve('./lib/skeletonEntryTemplate.js'),
'utf-8'
).catch(err => {
console.error(colors.red(
`${package.name} err: read ${require.resolve(
'./lib/skeletonEntryTemplate.js'
)} ${err}`
));
throw new Error(err);
});
await writeFile(
require.resolve('./lib/skeleton.entry.js'),
skeletonEntryTempalte.replace(
'SKELETON_ENTRY_RELATIVE_PATH',
`'${SKELETON_ENTRY_RELATIVE_PATH}'`
)
).catch(err => {
console.error(colors.red(
`${package.name} err: write ${require.resolve(
'./lib/skeleton.entry.js'
)} ${err}`
));
throw new Error(err);
});
console.info(colors.green(
`${package.name} info: write skeleton entry successfully!`
));
} else {
await ensureFile(
path.resolve(cliAbsolutePath, './lib/skeleton.vue')
).catch(err => {
console.error(colors.red(`${package.name} err: ${err}`));
throw new Error(err);
});
await copyFile(
sourceAbsolutePath,
require.resolve('./lib/skeleton.vue')
).catch(err => {
console.error(colors.red(
`${
package.name
} err: copy from ${sourceAbsolutePath} to ${require.resolve(
'./lib/skeleton.vue'
)} ${err}`
));
throw new Error(err);
});
console.info(colors.green(
`${package.name} info: copy skeleton template successfully!`
));
}
webpack(webpackConfig, (err, stats) => {
if (err || stats.hasErrors()) {
throw new Error(err || stats);
}
console.log(stats.toString({
colors: true
}));
// 读取`skeleton.json`,以`index.html`为模板写入内容
const renderer = createBundleRenderer(
require.resolve('./lib/dist/skeleton.json'),
{
template: fs.readFileSync(
require.resolve('./lib/index.html'),
'utf-8'
)
}
);
// 把上一步模板完成的内容写入(替换)`index.html`
renderer.renderToString({}, async (err, html) => {
await writeFile(targetAbsolutePath, html, 'utf-8').catch((err) => {
console.error(colors.red(
`${package.name} err: generate skeleton ${err}`
));
throw new Error(err);
});
console.info(colors.green(
`${package.name} info: generate skeleton successfully!\n`
));
console.info(colors.green(
`click ${targetAbsolutePath} to preview\n`
));
});
});
})
.on('--help', function() {
console.log('');
console.log('Examples:');
console.log('');
console.log(
' $ skeleton-cli gn -s <source path of skeleton entry> -t <target path of output skeleton html>'
);
});
program.on('command:*', function() {
console.error(colors.red(
'Invalid command: %s\nSee --help for a list of available commands.',
program.args.join(' ')
));
process.exit(1);
});
program.parse(process.argv);
if (process.argv.length < 3) program.help();