-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
154 lines (133 loc) · 5.11 KB
/
Copy pathindex.js
File metadata and controls
154 lines (133 loc) · 5.11 KB
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
#! /usr/bin/env node
const { readFileSync, writeFileSync, existsSync } = require('fs');
const { resolve, basename, dirname } = require('path');
const nunjucks = require('nunjucks');
const chokidar = require('chokidar');
const glob = require('glob');
const mkdirp = require('mkdirp');
const chalk = require('chalk').default;
require('dotenv').config();
const projectEnv = 'production' === process.env.NODE_ENV ? 'prod' : 'dev';
const engineEnv = require('./helpers/engine');
const { argv } = require('yargs')
.usage('Usage: codevault <file|glob|command> [context] [options]')
.example('codevault i -p @codevault/sql-logging', 'Generate package install script')
.example('codevault foo.tpl', 'Compile foo.tpl using default context file')
.example('codevault *.tpl -w -p src -o out', 'Watch .tpl files in ./src, compile them to ./out')
.epilogue('For more information on template engine: https://mozilla.github.io/nunjucks/')
.help()
.alias('help', 'h')
.locale('en')
.demandCommand()
.version(false)
.option('path', {
alias: 'p',
string: true,
requiresArg: true,
nargs: 1,
default: '.',
describe: 'Path where templates live'
})
.option('out', {
alias: 'o',
string: true,
requiresArg: true,
nargs: 1,
default: 'out',
describe: 'Output folder'
})
.option('watch', {
alias: 'w',
boolean: true,
describe: 'Watch files change, except files starting by "_"'
})
.option('extension', {
alias: 'e',
string: true,
requiresArg: true,
default: 'sql',
describe: 'Extension of the rendered files'
})
.option('options', {
alias: 'O',
string: true,
requiresArg: true,
nargs: 1,
describe: 'Options file'
});
const inputDir = argv.path === '.' ? resolve(process.cwd()) : dirname(require.resolve(argv.path));
// let packageName = (argv._[0] === ('install' || 'test' || 'doc' || undefined) && argv.path) || undefined;
let packageName = (['install', 'test', 'doc', 'i', 't', 'd'].includes(argv._[0]) && argv.path) || undefined;
let fileMask = ['install', 'i'].includes(argv._[0])
? basename(require.resolve(argv.path))
: ['test', 't'].includes(argv._[0])
? '*.test.*.template'
: ['doc', 'd'].includes(argv._[0])
? '*.doc.*.template'
: argv._[0];
console.warn('argv._[0]', argv._[0], 'argv.path', argv.path);
console.warn('fileMask', fileMask, 'packageName', packageName);
// let fileMask = argv._[0] || basename(require.resolve(argv.path))
// let packageName = ((argv._[0] === undefined) && argv.path) || undefined
// console.warn('fileMask',fileMask,'packageName',packageName)
const outputDir = packageName ? `${argv.out}/${packageName}` : argv.out || '';
console.warn('outputDir', outputDir);
let contextFile = (projectEnv === 'dev' && 'local.context.json') || (projectEnv === 'prod' && 'context.json');
console.log(chalk.blue(`Environment: [${projectEnv}] ${contextFile || chalk.red.bold('context file not defined')}`));
context = existsSync(contextFile) ? JSON.parse(readFileSync(contextFile, 'utf8')) : {};
// Expose environment variables to render context
context.env = context.env ? process.env : {};
/** @type {nunjucks.ConfigureOptions} */
const nunjucksOptions = argv.options
? JSON.parse(readFileSync(argv.options, 'utf8'))
: { lstripBlocks: true, noCache: true, autoescape: true };
const codeVaultEnv = engineEnv.init(inputDir, nunjucksOptions, packageName);
const render = (/** @type {string[]} */ files) => {
console.time('Execution time');
console.group('Rendering');
for (const file of files) {
console.log(chalk.blue('File: ' + file));
try {
const res = codeVaultEnv.render(file, context);
// let outputFile = file.replace(/\.\w+$/, `.${argv.extension}`)
let outputFile = file.replace('.template', '');
console.log(chalk.white('Output: ' + outputFile));
if (outputDir) {
outputFile = resolve(outputDir, outputFile);
mkdirp.sync(dirname(outputFile));
}
writeFileSync(outputFile, res);
} catch (err) {
console.log(chalk.red(err.message));
}
}
console.groupEnd();
console.timeEnd('Execution time');
};
/** @type {glob.IOptions} */
const globOptions = { strict: true, cwd: inputDir, ignore: '**/_*.*', nonull: true };
// Render the files given a glob pattern (except the ones starting with "_")
glob(fileMask, globOptions, (err, files) => {
if (err) return console.error(chalk.red(err));
render(files);
});
// Watcher
if (argv.watch) {
const layouts = [];
const templates = [];
/** @type {chokidar.WatchOptions} */
const watchOptions = { persistent: true, cwd: inputDir };
const watcher = chokidar.watch(fileMask, watchOptions);
// const watcher = chokidar.watch(argv._[0], watchOptions);
watcher.on('ready', () => console.log(chalk.gray('Watching templates...')));
// Sort files to not render partials/layouts
watcher.on('add', file => {
if (basename(file).indexOf('_') === 0) layouts.push(file);
else templates.push(file);
});
// if the file is a layout/partial, render all other files instead
watcher.on('change', file => {
if (layouts.indexOf(file) > -1) render(templates, context);
else render([file], context);
});
}