forked from haginus/tw-labs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
90 lines (86 loc) · 2.53 KB
/
scripts.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
#!/usr/bin/env node
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
const fs = require('fs');
const path = require('path');
yargs(hideBin(process.argv))
.command(
'generate-gist [gistId]',
'Generate a gist.',
(yargs) => (
yargs
.positional('gistId', {
describe: 'ID to for the gist'
})
),
(argv) => {
const gistName = argv.gistId.split('/').pop();
const htmlFiles = getNameArray(argv.html, gistName).map(name => nameToFileObject(name, "html"));
const cssFiles = getNameArray(argv.css, gistName).map(name => nameToFileObject(name, "css"));
const jsFiles = getNameArray(argv.js, gistName).map(name => nameToFileObject(name, "js"));
const tsFiles = getNameArray(argv.ts, gistName).map(name => nameToFileObject(name, "ts"));
const files = [...htmlFiles, ...cssFiles, ...jsFiles, ...tsFiles];
const json = {
title: argv.title || null,
showFileNames: argv.fileNames,
result: argv.result,
files,
}
const dir = "src/assets/gists";
const gistPath = path.resolve(__dirname, dir, argv.gistId);
fs.mkdirSync(gistPath, { recursive: true });
fs.writeFileSync(path.resolve(gistPath, 'index.json'), JSON.stringify(json, null, 2));
files.forEach(file => {
fs.writeFileSync(path.resolve(gistPath, file.name), '', { flag: 'a' });
});
}
)
.option('title', {
alias: 't',
type: 'string',
description: 'Title of the gist'
})
.option('fileNames', {
alias: 'fn',
type: 'boolean',
default: false,
description: 'Show or hide names of files.'
})
.option('result', {
alias: 'r',
type: 'boolean',
default: false,
description: 'Show result or not'
})
.option('html', {
alias: 'html',
type: 'array',
description: 'HTML Files. Leave flag empty for generating a single file.'
})
.option('css', {
alias: 'css',
type: 'array',
description: 'CSS Files. Leave flag empty for generating a single file.'
})
.option('js', {
alias: 'js',
type: 'array',
description: 'JS Files. Leave flag empty for generating a single file.'
})
.option('ts', {
alias: 'ts',
type: 'array',
description: 'TS Files. Leave flag empty for generating a single file.'
})
.parse()
function nameToFileObject(name, fileType) {
return {
name: `${name}.${fileType}`,
fileType,
}
}
function getNameArray(arr, defaultName) {
if(!arr) return [];
if(arr.length == 0) return [ defaultName ];
return arr;
}