@salesforce/cli 2.35.2-qa.0 → 2.35.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,81 @@
1
+ const hook = async function ({ argv, options, context }) {
2
+ // Skip this hook if command does not have a --flags-dir flag or if it is not present in argv
3
+ if (!argv.includes('--flags-dir') || !options.flags?.['flags-dir'])
4
+ return argv;
5
+ const flagsDir = argv[argv.indexOf('--flags-dir') + 1];
6
+ if (!flagsDir) {
7
+ context.debug('No flags dir provided');
8
+ return argv;
9
+ }
10
+ if (flagsDir.startsWith('-')) {
11
+ context.debug(`No flags dir provided, got ${flagsDir}`);
12
+ return argv;
13
+ }
14
+ const { default: fs } = await import('node:fs/promises');
15
+ const { default: path } = await import('node:path');
16
+ context.debug('Initial argv', argv.join(' '));
17
+ const flagsToIgnore = new Set(Object.entries(options.flags ?? {})
18
+ .filter(([, flagOptions]) =>
19
+ // don't ignore if flag can take multiple values
20
+ (flagOptions.type === 'option' && flagOptions.multiple !== true) || flagOptions.type === 'boolean')
21
+ .filter(([flagName, flagOptions]) =>
22
+ // ignore if short char flag is present
23
+ argv.includes(`-${flagOptions.char}`) ||
24
+ // ignore if long flag is present
25
+ argv.includes(`--${flagName}`) ||
26
+ // ignore if --no- flag is present
27
+ (flagOptions.type === 'boolean' && flagOptions.allowNo && argv.includes(`--no-${flagName}`)))
28
+ .flatMap(([flagName, flagOptions]) => {
29
+ // Also ignore the --no- flag if boolean flag allows it
30
+ if (flagOptions.type === 'boolean' && flagOptions.allowNo) {
31
+ return [flagName, `no-${flagName}`];
32
+ }
33
+ return [flagName];
34
+ }));
35
+ context.debug('Flags to ignore', flagsToIgnore);
36
+ async function safeReadDir(filePath) {
37
+ try {
38
+ return await fs.readdir(filePath);
39
+ }
40
+ catch (err) {
41
+ context.debug('No flags dir found');
42
+ context.debug(err);
43
+ return [];
44
+ }
45
+ }
46
+ async function safeReadFile(filePath) {
47
+ try {
48
+ return await fs.readFile(filePath, 'utf8');
49
+ }
50
+ catch (err) {
51
+ context.debug(filePath, err);
52
+ }
53
+ }
54
+ const filesInDir = await safeReadDir(flagsDir);
55
+ context.debug('Files in dir', filesInDir);
56
+ const flagsToInsert = await Promise.all(filesInDir
57
+ // ignore files that were provided as flags
58
+ .filter((f) => !flagsToIgnore.has(f))
59
+ .map(async (file) => {
60
+ const { name, ext } = path.parse(file);
61
+ const contents = await safeReadFile(path.join(flagsDir, file));
62
+ if (contents === undefined) {
63
+ return [name, undefined];
64
+ }
65
+ const values = ext === '.json' ? [JSON.stringify(JSON.parse(contents))] : contents?.trim().split('\n');
66
+ return [name, values];
67
+ }));
68
+ const newArgv = [...argv];
69
+ context.debug('Flags to insert', flagsToInsert);
70
+ for (const [flag, values] of flagsToInsert) {
71
+ for (const value of values ?? []) {
72
+ newArgv.push(flag.length === 1 ? `-${flag}` : `--${flag}`);
73
+ if (value)
74
+ newArgv.push(value);
75
+ }
76
+ }
77
+ context.debug(`Returning argv: ${newArgv.join(' ')}`);
78
+ return newArgv;
79
+ };
80
+ export default hook;
81
+ //# sourceMappingURL=preparse.js.map