-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathcreate.ts
126 lines (111 loc) · 4.09 KB
/
create.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
126
// @ts-nocheck
/**
* To add a new asset type: See vue-app.js for a simple example
* 1. Create a new file under the `create` directory with the command name being the name of the file
* 2. The file _must_ export a `dest` function and an `execute` function. See below for details
* Note: No changes should be needed to this file when adding new types, unless you need new functionality
*
* @export {(Data) => String} dest - A function returning the destination of the asset we are creating
* @export {(Data) => Object=} execute - The code called once all other checks pass. This should contain the logic
* for handling your command. Optionally return an object containing KV pairs
* of any context or data you want passed along to usage tracking
* @export {Boolean=} hidden - If true, the command will not show up in --help
* @export {(Data) => Boolean=} validate - If provided, return true if it passes validation, false otherwise.
* If not provided, validation will automatically succeed
*
* The Data object contains
* {
* assetType: String - Type of the asset (e.g. api-sample, react-app, template)
* name: String - Filename of the asset
* dest: String - The path specified by the user on where to create the asset
* internal: Boolean - A flag for retrieving the internal spec for the asset type
* options: Object - The options object passed to the command by Yargs
* }
*/
const fs = require('fs-extra');
const { logError } = require('../lib/errorHandlers/index');
const { logger } = require('@hubspot/local-dev-lib/logger');
const {
setLogLevel,
addGlobalOptions,
addConfigOptions,
} = require('../lib/commonOpts');
const { resolveLocalPath } = require('../lib/filesystem');
const { trackCommandUsage } = require('../lib/usageTracking');
const assets = require('./create/index');
const { i18n } = require('../lib/lang');
const SUPPORTED_ASSET_TYPES = Object.keys(assets)
.filter(t => !assets[t].hidden)
.join(', ');
exports.command = 'create <type> [name] [dest]';
exports.describe = i18n(`commands.create.describe`, {
supportedAssetTypes: SUPPORTED_ASSET_TYPES,
});
exports.handler = async options => {
let { type: assetType, dest } = options;
const { name, internal: getInternalVersion } = options;
setLogLevel(options);
assetType = typeof assetType === 'string' && assetType.toLowerCase();
if (assetType === 'global-partial') {
logger.error(
i18n(`commands.create.errors.deprecatedAssetType`, {
assetType,
newCommand: 'hs create template',
type: 'global partial',
})
);
return;
}
if (!assetType || !assets[assetType]) {
logger.error(
i18n(`commands.create.errors.unsupportedAssetType`, {
assetType,
supportedAssetTypes: SUPPORTED_ASSET_TYPES,
})
);
return;
}
const asset = assets[assetType];
const argsToPass = { assetType, name, dest, getInternalVersion, options };
dest = argsToPass.dest = resolveLocalPath(asset.dest(argsToPass));
const { derivedAccountId } = options;
trackCommandUsage('create', { assetType }, derivedAccountId);
try {
await fs.ensureDir(dest);
} catch (e) {
logger.error(
i18n(`commands.create.errors.unusablePath`, {
path: dest,
})
);
logError(e, {
filepath: dest,
operation: 'write',
});
return;
}
if (asset.validate && !asset.validate(argsToPass)) return;
await asset.execute(argsToPass);
};
exports.builder = yargs => {
yargs.positional('type', {
describe: i18n(`commands.create.positionals.type.describe`),
type: 'string',
});
yargs.positional('name', {
describe: i18n(`commands.create.positionals.name.describe`),
type: 'string',
});
yargs.positional('dest', {
describe: i18n(`commands.create.positionals.dest.describe`),
type: 'string',
});
yargs.option('internal', {
describe: 'Internal HubSpot version of creation command',
type: 'boolean',
hidden: true,
});
addConfigOptions(yargs);
addGlobalOptions(yargs);
return yargs;
};