-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
125 lines (106 loc) · 3.51 KB
/
index.ts
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
/**
* Copyright Zendesk, Inc.
*
* Use of this source code is governed under the Apache License, Version 2.0
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/
import { handleErrorMessage, handleSuccessMessage } from '../../utils/index.js';
import { readFile, rename, writeFile } from 'node:fs/promises';
import { Command } from 'commander';
import { Ora } from 'ora';
import { copy } from 'fs-extra';
import { default as handlebars } from 'handlebars';
import { default as helpers } from 'handlebars-helpers';
import lodash from 'lodash';
import pluralize from 'pluralize';
import { resolve } from 'node:path';
import { default as walk } from 'klaw';
/**
* Register handlebars helpers.
*/
const registerHelpers = (): void => {
helpers({ handlebars });
handlebars.registerHelper('kebabcase', (string: string) => lodash.kebabCase(string));
handlebars.registerHelper('pluralize', (string: string) => pluralize.plural(string));
handlebars.registerHelper('singularize', (string: string) => pluralize.singular(string));
handlebars.registerHelper('snakecase', (string: string) => lodash.snakeCase(string));
handlebars.registerHelper('startcase', (string: string) => lodash.startCase(string));
};
interface ILernaNewArgs {
src: string;
dest: string;
tags?: Record<string, string>;
spinner?: Ora;
}
type RETVAL = {
src: string;
dest: string;
};
/**
* Execute the `lerna-new` command.
*
* @param {string} args.src Source directory.
* @param {string} args.dest Destination directory.
* @param {object} args.tags Handlebars template name-value pairs.
* @param {string} [args.spinner] Terminal spinner.
*
* @returns {object} The resolved source and destination paths.
*/
export const execute = async (args: ILernaNewArgs): Promise<RETVAL | undefined> => {
const retVal = {
src: resolve(args.src),
dest: resolve(args.dest)
};
try {
await copy(retVal.src, retVal.dest, { overwrite: false, errorOnExist: true });
registerHelpers();
for await (const file of walk(retVal.dest)) {
const path = handlebars.compile(file.path)(args.tags);
if (file.path !== path) {
await rename(file.path, path);
}
if (!file.stats.isDirectory()) {
const content = await readFile(path, 'utf8');
const data = handlebars.compile(content)(args.tags);
await writeFile(path, data, 'utf8');
}
}
} catch (error: unknown) {
handleErrorMessage(error, 'lerna-new', args.spinner);
throw error;
}
return retVal;
};
export default (spinner: Ora): Command => {
const command = new Command('lerna-new');
return command
.description('generate a new package from a template source directory')
.argument('<src>', 'template source directory')
.argument('<dest>', 'package destination directory')
.option(
'-t --tag <tags...>',
'{{Handlebars}} template <name>=<value> tags',
(tag: string, retVal: Record<string, string> = {}) => {
const [name, value] = tag.split('=');
retVal[name] = value;
return retVal;
}
)
.action(async (src, dest) => {
try {
spinner.start();
const tags = command.opts().tag;
const result = await execute({ src, dest, tags, spinner });
if (result) {
handleSuccessMessage(`${result.src} -> ${result.dest}`, spinner);
} else {
throw new Error();
}
} catch {
spinner.fail('Unable to generate new package');
process.exitCode = 1;
} finally {
spinner.stop();
}
});
};