@virmator/core 13.0.0
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.
- package/LICENSE-CC0 +121 -0
- package/LICENSE-MIT +21 -0
- package/README.md +5 -0
- package/dist/api/copy-configs.d.ts +22 -0
- package/dist/api/copy-configs.js +74 -0
- package/dist/api/execute-command.d.ts +60 -0
- package/dist/api/execute-command.js +269 -0
- package/dist/api/index.d.ts +5 -0
- package/dist/api/index.js +5 -0
- package/dist/api/install-deps.d.ts +25 -0
- package/dist/api/install-deps.js +79 -0
- package/dist/api/parse-args.d.ts +35 -0
- package/dist/api/parse-args.js +111 -0
- package/dist/api/typescript-config-file.d.ts +30 -0
- package/dist/api/typescript-config-file.js +66 -0
- package/dist/api/virmator-flags.d.ts +21 -0
- package/dist/api/virmator-flags.js +19 -0
- package/dist/augments/fs/search.d.ts +9 -0
- package/dist/augments/fs/search.js +31 -0
- package/dist/augments/index.d.ts +4 -0
- package/dist/augments/index.js +4 -0
- package/dist/augments/object/access.d.ts +3 -0
- package/dist/augments/object/access.js +13 -0
- package/dist/augments/stream/callback-writable.d.ts +10 -0
- package/dist/augments/stream/callback-writable.js +21 -0
- package/dist/augments/tsconfig/parse-tsconfig.d.ts +3 -0
- package/dist/augments/tsconfig/parse-tsconfig.js +24 -0
- package/dist/colors.d.ts +10 -0
- package/dist/colors.js +19 -0
- package/dist/errors/index.d.ts +1 -0
- package/dist/errors/index.js +1 -0
- package/dist/errors/virmator-no-trace.error.d.ts +13 -0
- package/dist/errors/virmator-no-trace.error.js +15 -0
- package/dist/file-paths.mock.d.ts +3 -0
- package/dist/file-paths.mock.js +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/plugin/index.d.ts +6 -0
- package/dist/plugin/index.js +6 -0
- package/dist/plugin/plugin-configs.d.ts +31 -0
- package/dist/plugin/plugin-configs.js +1 -0
- package/dist/plugin/plugin-env.d.ts +16 -0
- package/dist/plugin/plugin-env.js +18 -0
- package/dist/plugin/plugin-executor.d.ts +104 -0
- package/dist/plugin/plugin-executor.js +1 -0
- package/dist/plugin/plugin-init.d.ts +45 -0
- package/dist/plugin/plugin-init.js +6 -0
- package/dist/plugin/plugin-logger.d.ts +10 -0
- package/dist/plugin/plugin-logger.js +89 -0
- package/dist/plugin/plugin.d.ts +7 -0
- package/dist/plugin/plugin.js +16 -0
- package/package.json +54 -0
package/LICENSE-CC0
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
CC0 1.0 Universal
|
|
2
|
+
|
|
3
|
+
Creative Commons Legal Code
|
|
4
|
+
|
|
5
|
+
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
|
|
6
|
+
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
|
|
7
|
+
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
|
|
8
|
+
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
|
|
9
|
+
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
|
|
10
|
+
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
|
|
11
|
+
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
|
|
12
|
+
HEREUNDER.
|
|
13
|
+
|
|
14
|
+
Statement of Purpose
|
|
15
|
+
|
|
16
|
+
The laws of most jurisdictions throughout the world automatically confer
|
|
17
|
+
exclusive Copyright and Related Rights (defined below) upon the creator
|
|
18
|
+
and subsequent owner(s) (each and all, an "owner") of an original work of
|
|
19
|
+
authorship and/or a database (each, a "Work").
|
|
20
|
+
|
|
21
|
+
Certain owners wish to permanently relinquish those rights to a Work for
|
|
22
|
+
the purpose of contributing to a commons of creative, cultural and
|
|
23
|
+
scientific works ("Commons") that the public can reliably and without fear
|
|
24
|
+
of later claims of infringement build upon, modify, incorporate in other
|
|
25
|
+
works, reuse and redistribute as freely as possible in any form whatsoever
|
|
26
|
+
and for any purposes, including without limitation commercial purposes.
|
|
27
|
+
These owners may contribute to the Commons to promote the ideal of a free
|
|
28
|
+
culture and the further production of creative, cultural and scientific
|
|
29
|
+
works, or to gain reputation or greater distribution for their Work in
|
|
30
|
+
part through the use and efforts of others.
|
|
31
|
+
|
|
32
|
+
For these and/or other purposes and motivations, and without any
|
|
33
|
+
expectation of additional consideration or compensation, the person
|
|
34
|
+
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
|
|
35
|
+
is an owner of Copyright and Related Rights in the Work, voluntarily
|
|
36
|
+
elects to apply CC0 to the Work and publicly distribute the Work under its
|
|
37
|
+
terms, with knowledge of his or her Copyright and Related Rights in the
|
|
38
|
+
Work and the meaning and intended legal effect of CC0 on those rights.
|
|
39
|
+
|
|
40
|
+
1. Copyright and Related Rights. A Work made available under CC0 may be
|
|
41
|
+
protected by copyright and related or neighboring rights ("Copyright and
|
|
42
|
+
Related Rights"). Copyright and Related Rights include, but are not
|
|
43
|
+
limited to, the following:
|
|
44
|
+
|
|
45
|
+
i. the right to reproduce, adapt, distribute, perform, display,
|
|
46
|
+
communicate, and translate a Work;
|
|
47
|
+
ii. moral rights retained by the original author(s) and/or performer(s);
|
|
48
|
+
iii. publicity and privacy rights pertaining to a person's image or
|
|
49
|
+
likeness depicted in a Work;
|
|
50
|
+
iv. rights protecting against unfair competition in regards to a Work,
|
|
51
|
+
subject to the limitations in paragraph 4(a), below;
|
|
52
|
+
v. rights protecting the extraction, dissemination, use and reuse of data
|
|
53
|
+
in a Work;
|
|
54
|
+
vi. database rights (such as those arising under Directive 96/9/EC of the
|
|
55
|
+
European Parliament and of the Council of 11 March 1996 on the legal
|
|
56
|
+
protection of databases, and under any national implementation
|
|
57
|
+
thereof, including any amended or successor version of such
|
|
58
|
+
directive); and
|
|
59
|
+
vii. other similar, equivalent or corresponding rights throughout the
|
|
60
|
+
world based on applicable law or treaty, and any national
|
|
61
|
+
implementations thereof.
|
|
62
|
+
|
|
63
|
+
2. Waiver. To the greatest extent permitted by, but not in contravention
|
|
64
|
+
of, applicable law, Affirmer hereby overtly, fully, permanently,
|
|
65
|
+
irrevocably and unconditionally waives, abandons, and surrenders all of
|
|
66
|
+
Affirmer's Copyright and Related Rights and associated claims and causes
|
|
67
|
+
of action, whether now known or unknown (including existing as well as
|
|
68
|
+
future claims and causes of action), in the Work (i) in all territories
|
|
69
|
+
worldwide, (ii) for the maximum duration provided by applicable law or
|
|
70
|
+
treaty (including future time extensions), (iii) in any current or future
|
|
71
|
+
medium and for any number of copies, and (iv) for any purpose whatsoever,
|
|
72
|
+
including without limitation commercial, advertising or promotional
|
|
73
|
+
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
|
|
74
|
+
member of the public at large and to the detriment of Affirmer's heirs and
|
|
75
|
+
successors, fully intending that such Waiver shall not be subject to
|
|
76
|
+
revocation, rescission, cancellation, termination, or any other legal or
|
|
77
|
+
equitable action to disrupt the quiet enjoyment of the Work by the public
|
|
78
|
+
as contemplated by Affirmer's express Statement of Purpose.
|
|
79
|
+
|
|
80
|
+
3. Public License Fallback. Should any part of the Waiver for any reason
|
|
81
|
+
be judged legally invalid or ineffective under applicable law, then the
|
|
82
|
+
Waiver shall be preserved to the maximum extent permitted taking into
|
|
83
|
+
account Affirmer's express Statement of Purpose. In addition, to the
|
|
84
|
+
extent the Waiver is so judged Affirmer hereby grants to each affected
|
|
85
|
+
person a royalty-free, non transferable, non sublicensable, non exclusive,
|
|
86
|
+
irrevocable and unconditional license to exercise Affirmer's Copyright and
|
|
87
|
+
Related Rights in the Work (i) in all territories worldwide, (ii) for the
|
|
88
|
+
maximum duration provided by applicable law or treaty (including future
|
|
89
|
+
time extensions), (iii) in any current or future medium and for any number
|
|
90
|
+
of copies, and (iv) for any purpose whatsoever, including without
|
|
91
|
+
limitation commercial, advertising or promotional purposes (the
|
|
92
|
+
"License"). The License shall be deemed effective as of the date CC0 was
|
|
93
|
+
applied by Affirmer to the Work. Should any part of the License for any
|
|
94
|
+
reason be judged legally invalid or ineffective under applicable law, such
|
|
95
|
+
partial invalidity or ineffectiveness shall not invalidate the remainder
|
|
96
|
+
of the License, and in such case Affirmer hereby affirms that he or she
|
|
97
|
+
will not (i) exercise any of his or her remaining Copyright and Related
|
|
98
|
+
Rights in the Work or (ii) assert any associated claims and causes of
|
|
99
|
+
action with respect to the Work, in either case contrary to Affirmer's
|
|
100
|
+
express Statement of Purpose.
|
|
101
|
+
|
|
102
|
+
4. Limitations and Disclaimers.
|
|
103
|
+
|
|
104
|
+
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
|
105
|
+
surrendered, licensed or otherwise affected by this document.
|
|
106
|
+
b. Affirmer offers the Work as-is and makes no representations or
|
|
107
|
+
warranties of any kind concerning the Work, express, implied,
|
|
108
|
+
statutory or otherwise, including without limitation warranties of
|
|
109
|
+
title, merchantability, fitness for a particular purpose, non
|
|
110
|
+
infringement, or the absence of latent or other defects, accuracy, or
|
|
111
|
+
the present or absence of errors, whether or not discoverable, all to
|
|
112
|
+
the greatest extent permissible under applicable law.
|
|
113
|
+
c. Affirmer disclaims responsibility for clearing rights of other persons
|
|
114
|
+
that may apply to the Work or any use thereof, including without
|
|
115
|
+
limitation any person's Copyright and Related Rights in the Work.
|
|
116
|
+
Further, Affirmer disclaims responsibility for obtaining any necessary
|
|
117
|
+
consents, permissions or other rights required for any use of the
|
|
118
|
+
Work.
|
|
119
|
+
d. Affirmer understands and acknowledges that Creative Commons is not a
|
|
120
|
+
party to this document and has no duty or obligation with respect to
|
|
121
|
+
this CC0 or use of the Work.
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 electrovir
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { MaybePromise } from '@augment-vir/common';
|
|
2
|
+
import { VirmatorPluginResolvedConfigFile } from '../plugin/plugin-configs';
|
|
3
|
+
import { PackageType } from '../plugin/plugin-env';
|
|
4
|
+
import { MonoRepoPackage, UsedVirmatorPluginCommands, VirmatorPluginResolvedConfigs } from '../plugin/plugin-executor';
|
|
5
|
+
import { PluginLogger } from '../plugin/plugin-logger';
|
|
6
|
+
/**
|
|
7
|
+
* Extracts a 1d array of virmator plugin config files from previously calculated used commands and
|
|
8
|
+
* resolved configs.
|
|
9
|
+
*/
|
|
10
|
+
export declare function flattenConfigs(usedCommands: Readonly<UsedVirmatorPluginCommands<any>>, resolvedConfigs: Readonly<VirmatorPluginResolvedConfigs<any>>): VirmatorPluginResolvedConfigFile[];
|
|
11
|
+
/** Copies a plugin's entire set of configs based on the used command. */
|
|
12
|
+
export declare function copyPluginConfigs(usedCommands: Readonly<UsedVirmatorPluginCommands>, resolvedConfigs: Readonly<VirmatorPluginResolvedConfigs<any>>, packageType: PackageType, monoRepoPackages: MonoRepoPackage[], log: PluginLogger, filteredArgs: string[]): Promise<void>;
|
|
13
|
+
/** Copies a single virmator plugin config file. */
|
|
14
|
+
export declare function copyConfigFile(config: Readonly<Pick<VirmatorPluginResolvedConfigFile, 'fullCopyFromPath' | 'fullCopyToPath'>>, log: PluginLogger,
|
|
15
|
+
/**
|
|
16
|
+
* If `true`, the config will be copied even if the copy destination already exists.
|
|
17
|
+
*
|
|
18
|
+
* @default false
|
|
19
|
+
*/
|
|
20
|
+
force?: boolean, transform?: ((currentContents: string) => MaybePromise<string>) | undefined,
|
|
21
|
+
/** Package name when this is being run within a sub-package. */
|
|
22
|
+
packageName?: string | undefined): Promise<void>;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { awaitedForEach, extractErrorMessage } from '@augment-vir/common';
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { basename, dirname, join } from 'node:path';
|
|
5
|
+
import { PackageType } from '../plugin/plugin-env';
|
|
6
|
+
/**
|
|
7
|
+
* Extracts a 1d array of virmator plugin config files from previously calculated used commands and
|
|
8
|
+
* resolved configs.
|
|
9
|
+
*/
|
|
10
|
+
export function flattenConfigs(usedCommands, resolvedConfigs) {
|
|
11
|
+
return Object.entries(usedCommands).flatMap(([commandName, usedCommand,]) => {
|
|
12
|
+
const commandConfigs = resolvedConfigs[commandName];
|
|
13
|
+
const currentConfigs = Object.values(commandConfigs?.configs || {});
|
|
14
|
+
const nestedConfigs = usedCommand?.subCommands
|
|
15
|
+
? flattenConfigs(usedCommand.subCommands, commandConfigs?.subCommands || {})
|
|
16
|
+
: [];
|
|
17
|
+
return [
|
|
18
|
+
...currentConfigs,
|
|
19
|
+
...nestedConfigs,
|
|
20
|
+
];
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/** Copies a plugin's entire set of configs based on the used command. */
|
|
24
|
+
export async function copyPluginConfigs(usedCommands, resolvedConfigs, packageType, monoRepoPackages, log, filteredArgs) {
|
|
25
|
+
const configs = flattenConfigs(usedCommands, resolvedConfigs).sort((a, b) => basename(a.copyToPath).localeCompare(basename(b.copyToPath)));
|
|
26
|
+
await awaitedForEach(Object.values(configs), async (config) => {
|
|
27
|
+
if ((config.configFlags || []).some((configFlag) => filteredArgs.includes(configFlag))) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
else if (packageType === PackageType.MonoRoot &&
|
|
31
|
+
!config.packageType.includes(PackageType.MonoRoot) &&
|
|
32
|
+
config.packageType.includes(PackageType.MonoPackage)) {
|
|
33
|
+
await Promise.all(monoRepoPackages.map(async (repoPackage) => {
|
|
34
|
+
await copyConfigFile({
|
|
35
|
+
...config,
|
|
36
|
+
fullCopyToPath: join(repoPackage.fullPath, config.copyToPath),
|
|
37
|
+
}, log);
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
else if (!config.required || !config.packageType.includes(packageType)) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
await copyConfigFile(config, log);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/** Copies a single virmator plugin config file. */
|
|
49
|
+
export async function copyConfigFile(config, log,
|
|
50
|
+
/**
|
|
51
|
+
* If `true`, the config will be copied even if the copy destination already exists.
|
|
52
|
+
*
|
|
53
|
+
* @default false
|
|
54
|
+
*/
|
|
55
|
+
force = false, transform,
|
|
56
|
+
/** Package name when this is being run within a sub-package. */
|
|
57
|
+
packageName) {
|
|
58
|
+
if (existsSync(config.fullCopyToPath) && !force) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const logPrefix = packageName ? `[${packageName}] ` : '';
|
|
62
|
+
const baseConfigFileName = basename(config.fullCopyToPath);
|
|
63
|
+
try {
|
|
64
|
+
const copyFromContents = (await readFile(config.fullCopyFromPath)).toString();
|
|
65
|
+
const writeContents = transform ? await transform(copyFromContents) : copyFromContents;
|
|
66
|
+
await mkdir(dirname(config.fullCopyToPath), { recursive: true });
|
|
67
|
+
await writeFile(config.fullCopyToPath, writeContents);
|
|
68
|
+
log.info(`${logPrefix}Copied ${baseConfigFileName}`);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
log.error(`${logPrefix}Failed to copy ${baseConfigFileName}: ${extractErrorMessage(error)}`);
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { PartialAndUndefined } from '@augment-vir/common';
|
|
2
|
+
import { VirmatorPlugin } from '../plugin/plugin';
|
|
3
|
+
import { PluginLogger } from '../plugin/plugin-logger';
|
|
4
|
+
/** Params for executing a plugin command. */
|
|
5
|
+
export type ExecuteCommandParams = {
|
|
6
|
+
/**
|
|
7
|
+
* An array of {@link VirmatorPlugin} definitions to use. There is no default list of plugins
|
|
8
|
+
* used here when called programmatically. However, the `virmator` npm package exports
|
|
9
|
+
* `defaultVirmatorPlugins` which could be passed in here.
|
|
10
|
+
*/
|
|
11
|
+
plugins: ReadonlyArray<Readonly<VirmatorPlugin>>;
|
|
12
|
+
/**
|
|
13
|
+
* The CLI command to execute either as a single string (like `process.argv`) or with each part
|
|
14
|
+
* of the command in an array. This will match exactly any commands passed to the `virmator`
|
|
15
|
+
* CLI. The virmator bin name does not need to be included when calling this programmatically.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* 'compile';
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* 'virmator compile';
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* [
|
|
25
|
+
* 'virmator',
|
|
26
|
+
* 'compile',
|
|
27
|
+
* ];
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* [
|
|
31
|
+
* 'compile',
|
|
32
|
+
* ];
|
|
33
|
+
*/
|
|
34
|
+
cliCommand: string | ReadonlyArray<string>;
|
|
35
|
+
} & PartialAndUndefined<{
|
|
36
|
+
/**
|
|
37
|
+
* The current working directory. In most cases, this can be left unset.
|
|
38
|
+
*
|
|
39
|
+
* @default process.cwd()
|
|
40
|
+
*/
|
|
41
|
+
cwd: string;
|
|
42
|
+
/**
|
|
43
|
+
* The `__filename` of the CLI entry point. Omit this property if you are not calling
|
|
44
|
+
* {@link executeVirmatorCommand} from a CLI script.
|
|
45
|
+
*
|
|
46
|
+
* @default ''
|
|
47
|
+
*/
|
|
48
|
+
entryPointFilePath: string;
|
|
49
|
+
/** Set the logger for use with this execution. */
|
|
50
|
+
log: PluginLogger;
|
|
51
|
+
/**
|
|
52
|
+
* The maximum number of concurrent processes that can run at the same time when running a
|
|
53
|
+
* command per mono-repo sub package.
|
|
54
|
+
*
|
|
55
|
+
* @default number of CPU cores - 1, with a min of 1.
|
|
56
|
+
*/
|
|
57
|
+
concurrency: number;
|
|
58
|
+
}>;
|
|
59
|
+
/** The entry point to virmator. Runs a virmator plugin command. */
|
|
60
|
+
export declare function executeVirmatorCommand({ log: logParam, entryPointFilePath, cwd, ...params }: ExecuteCommandParams): Promise<void>;
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { awaitedBlockingMap, filterMap, isTruthy, mapObjectValues, wrapInTry, } from '@augment-vir/common';
|
|
2
|
+
import { logColors, LogOutputType, readPackageJson, runShellCommand } from '@augment-vir/node-js';
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
import concurrently from 'concurrently';
|
|
5
|
+
import { getRelativePosixPackagePathsInDependencyOrder } from 'mono-vir';
|
|
6
|
+
import { cpus } from 'node:os';
|
|
7
|
+
import { join, resolve } from 'node:path';
|
|
8
|
+
import { isRunTimeType } from 'run-time-assertions';
|
|
9
|
+
import { findClosestPackageDir } from '../augments/index';
|
|
10
|
+
import { CallbackWritable } from '../augments/stream/callback-writable';
|
|
11
|
+
import { getTerminalColor } from '../colors';
|
|
12
|
+
import { hideNoTraceTraces, VirmatorNoTraceError } from '../errors/virmator-no-trace.error';
|
|
13
|
+
import { PackageType } from '../plugin/plugin-env';
|
|
14
|
+
import { createPluginLogger } from '../plugin/plugin-logger';
|
|
15
|
+
import { copyPluginConfigs } from './copy-configs';
|
|
16
|
+
import { installNpmDeps, installPluginNpmDeps } from './install-deps';
|
|
17
|
+
import { parseCliArgs } from './parse-args';
|
|
18
|
+
function resolveConfigs({ cwdPackagePath, pluginPackagePath }, cliCommands) {
|
|
19
|
+
return mapObjectValues(cliCommands, (commandName, command) => {
|
|
20
|
+
return {
|
|
21
|
+
configs: mapObjectValues(command.configFiles || {}, (configName, config) => {
|
|
22
|
+
return {
|
|
23
|
+
...config,
|
|
24
|
+
fullCopyToPath: join(cwdPackagePath, config.copyToPath),
|
|
25
|
+
fullCopyFromPath: join(pluginPackagePath, config.copyFromPath),
|
|
26
|
+
};
|
|
27
|
+
}),
|
|
28
|
+
subCommands: resolveConfigs({ cwdPackagePath, pluginPackagePath }, command.subCommands || {}),
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
async function determinePackageType(cwdPackagePath, monoRepoRootPath, cwdPackageJson) {
|
|
33
|
+
try {
|
|
34
|
+
if (cwdPackageJson.workspaces &&
|
|
35
|
+
(isRunTimeType(cwdPackageJson.workspaces, 'array')
|
|
36
|
+
? cwdPackageJson.workspaces.length
|
|
37
|
+
: cwdPackageJson.workspaces.packages?.length)) {
|
|
38
|
+
return PackageType.MonoRoot;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
if (monoRepoRootPath !== cwdPackagePath) {
|
|
42
|
+
const parentPackages = await getMonoRepoPackages(monoRepoRootPath);
|
|
43
|
+
if (parentPackages.find((monoPackage) => {
|
|
44
|
+
return join(monoRepoRootPath, monoPackage.relativePath) === cwdPackagePath;
|
|
45
|
+
})) {
|
|
46
|
+
return PackageType.MonoPackage;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return PackageType.TopPackage;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
console.error(error);
|
|
54
|
+
/** Default to package package type. */
|
|
55
|
+
return PackageType.TopPackage;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async function getMonoRepoPackages(cwdPackagePath) {
|
|
59
|
+
const relativePackagePathsInOrder = await wrapInTry(() => getRelativePosixPackagePathsInDependencyOrder(cwdPackagePath), {
|
|
60
|
+
fallbackValue: [],
|
|
61
|
+
});
|
|
62
|
+
return await Promise.all(relativePackagePathsInOrder.map(async (packagePath) => {
|
|
63
|
+
const packageJson = await wrapInTry(() => readPackageJson(packagePath), {
|
|
64
|
+
fallbackValue: undefined,
|
|
65
|
+
});
|
|
66
|
+
return {
|
|
67
|
+
packageName: packageJson?.name || packagePath,
|
|
68
|
+
relativePath: packagePath,
|
|
69
|
+
fullPath: join(cwdPackagePath, packagePath),
|
|
70
|
+
};
|
|
71
|
+
}));
|
|
72
|
+
}
|
|
73
|
+
async function findMonoRepoDir(cwdPackagePath) {
|
|
74
|
+
const parentPackageDir = wrapInTry(() => findClosestPackageDir(resolve(cwdPackagePath, '..')), {
|
|
75
|
+
fallbackValue: undefined,
|
|
76
|
+
});
|
|
77
|
+
if (parentPackageDir) {
|
|
78
|
+
const parentPackages = await getMonoRepoPackages(parentPackageDir);
|
|
79
|
+
if (parentPackages.find((monoPackage) => {
|
|
80
|
+
return join(parentPackageDir, monoPackage.relativePath) === cwdPackagePath;
|
|
81
|
+
})) {
|
|
82
|
+
return parentPackageDir;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return cwdPackagePath;
|
|
86
|
+
}
|
|
87
|
+
function writeLog(arg, log, logType, extraOptions) {
|
|
88
|
+
const transformed = extraOptions?.logTransform?.[logType]
|
|
89
|
+
? extraOptions.logTransform[logType](arg)
|
|
90
|
+
: arg;
|
|
91
|
+
if (!transformed) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const finalLog = [
|
|
95
|
+
extraOptions?.logPrefix || '',
|
|
96
|
+
transformed.replace(/\n$/, ''),
|
|
97
|
+
].join('');
|
|
98
|
+
if (logType === LogOutputType.error) {
|
|
99
|
+
log.error(finalLog);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
log.plain(finalLog);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** The entry point to virmator. Runs a virmator plugin command. */
|
|
106
|
+
export async function executeVirmatorCommand({ log: logParam, entryPointFilePath = '', cwd = process.cwd(), ...params }) {
|
|
107
|
+
const log = logParam || createPluginLogger(process);
|
|
108
|
+
const args = parseCliArgs({
|
|
109
|
+
...params,
|
|
110
|
+
log,
|
|
111
|
+
entryPointFilePath,
|
|
112
|
+
});
|
|
113
|
+
const plugin = args.plugin;
|
|
114
|
+
if (!args.commands.length || !plugin) {
|
|
115
|
+
throw new VirmatorNoTraceError(`Missing valid command.`);
|
|
116
|
+
}
|
|
117
|
+
const cwdPackagePath = findClosestPackageDir(cwd);
|
|
118
|
+
const cwdPackageJson = await readPackageJson(cwdPackagePath);
|
|
119
|
+
const pluginPackagePath = plugin.pluginPackageRootPath;
|
|
120
|
+
const resolvedConfigs = resolveConfigs({ cwdPackagePath, pluginPackagePath }, plugin.cliCommands);
|
|
121
|
+
const monoRepoRootPath = await findMonoRepoDir(cwdPackagePath);
|
|
122
|
+
const packageType = await determinePackageType(cwdPackagePath, monoRepoRootPath, cwdPackageJson);
|
|
123
|
+
const monoRepoPackages = packageType === PackageType.MonoRoot ? await getMonoRepoPackages(cwdPackagePath) : [];
|
|
124
|
+
const outerMaxProcesses = params.concurrency || cpus().length - 1 || 1;
|
|
125
|
+
const filteredArgs = args.filteredCommandArgs.filter(isTruthy);
|
|
126
|
+
const executorParams = {
|
|
127
|
+
cliInputs: {
|
|
128
|
+
filteredArgs,
|
|
129
|
+
usedCommands: args.usedCommands,
|
|
130
|
+
},
|
|
131
|
+
log,
|
|
132
|
+
cwd,
|
|
133
|
+
package: {
|
|
134
|
+
cwdPackagePath,
|
|
135
|
+
monoRepoPackages,
|
|
136
|
+
packageType,
|
|
137
|
+
monoRepoRootPath,
|
|
138
|
+
cwdPackageJson: cwdPackageJson,
|
|
139
|
+
cwdValidPackageJson: cwdPackageJson.name && cwdPackageJson.version
|
|
140
|
+
? cwdPackageJson
|
|
141
|
+
: undefined,
|
|
142
|
+
},
|
|
143
|
+
configs: resolvedConfigs,
|
|
144
|
+
virmator: {
|
|
145
|
+
allPlugins: params.plugins,
|
|
146
|
+
pluginPackagePath,
|
|
147
|
+
},
|
|
148
|
+
async runShellCommand(command, options, extraOptions) {
|
|
149
|
+
const prefix = extraOptions?.logPrefix ? `${extraOptions.logPrefix} ` : '';
|
|
150
|
+
log.faint(`${prefix}> ${command}`);
|
|
151
|
+
const result = await runShellCommand(command, {
|
|
152
|
+
cwd,
|
|
153
|
+
shell: 'bash',
|
|
154
|
+
stderrCallback(stderr) {
|
|
155
|
+
writeLog(stderr, log, LogOutputType.error, extraOptions);
|
|
156
|
+
},
|
|
157
|
+
stdoutCallback(stdout) {
|
|
158
|
+
writeLog(stdout, log, LogOutputType.standard, extraOptions);
|
|
159
|
+
},
|
|
160
|
+
...options,
|
|
161
|
+
});
|
|
162
|
+
if (result.error) {
|
|
163
|
+
throw new VirmatorNoTraceError(extraOptions?.includeErrorMessage ? result.stderr : undefined);
|
|
164
|
+
}
|
|
165
|
+
return result;
|
|
166
|
+
},
|
|
167
|
+
async runInstallDeps(deps) {
|
|
168
|
+
await installNpmDeps({
|
|
169
|
+
deps,
|
|
170
|
+
cwdPackageJson,
|
|
171
|
+
cwdPackagePath,
|
|
172
|
+
log,
|
|
173
|
+
packageType,
|
|
174
|
+
pluginName: plugin.name,
|
|
175
|
+
pluginPackagePath,
|
|
176
|
+
});
|
|
177
|
+
},
|
|
178
|
+
async runPerPackage(generateCliCommandString, maxProcesses) {
|
|
179
|
+
if (packageType !== PackageType.MonoRoot) {
|
|
180
|
+
throw new Error(`Cannot run "runPerPackage" on non-mono-repo.`);
|
|
181
|
+
}
|
|
182
|
+
else if (!monoRepoPackages.length) {
|
|
183
|
+
throw new Error(`No mono-repo packages found. Make sure to set the 'workspaces' field in your mono-repo package.json and run 'npm i'.`);
|
|
184
|
+
}
|
|
185
|
+
const commands = (await awaitedBlockingMap(monoRepoPackages, async (monoRepoPackage, index) => {
|
|
186
|
+
const colorString = getTerminalColor(index);
|
|
187
|
+
const color = chalk[colorString];
|
|
188
|
+
const absolutePackagePath = join(cwd, monoRepoPackage.relativePath);
|
|
189
|
+
const command = await generateCliCommandString({
|
|
190
|
+
packageCwd: absolutePackagePath,
|
|
191
|
+
packageName: monoRepoPackage.packageName,
|
|
192
|
+
color,
|
|
193
|
+
});
|
|
194
|
+
if (!command) {
|
|
195
|
+
return undefined;
|
|
196
|
+
}
|
|
197
|
+
const prefix = color(`[${monoRepoPackage.packageName}]`);
|
|
198
|
+
log.faint(`${prefix}${logColors.faint} > ${command}`);
|
|
199
|
+
return {
|
|
200
|
+
command,
|
|
201
|
+
cwd: absolutePackagePath,
|
|
202
|
+
name: monoRepoPackage.packageName,
|
|
203
|
+
prefixColor: colorString,
|
|
204
|
+
};
|
|
205
|
+
})).filter(isTruthy);
|
|
206
|
+
const writeStream = new CallbackWritable(log);
|
|
207
|
+
/** Force concurrently to use color even though it's being run as a subscript. */
|
|
208
|
+
process.env.FORCE_COLOR = '1';
|
|
209
|
+
let concurrentlyResults = [];
|
|
210
|
+
let failed = false;
|
|
211
|
+
try {
|
|
212
|
+
if (commands.length) {
|
|
213
|
+
concurrentlyResults = await concurrently(commands, {
|
|
214
|
+
killOthers: 'failure',
|
|
215
|
+
outputStream: writeStream,
|
|
216
|
+
maxProcesses: maxProcesses || outerMaxProcesses,
|
|
217
|
+
}).result;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
failed = true;
|
|
222
|
+
if (Array.isArray(error)) {
|
|
223
|
+
concurrentlyResults = error;
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
throw error;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (!failed) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
const failedCommandNames = filterMap(concurrentlyResults, (result) => result.command.name, (commandName, result) => {
|
|
233
|
+
return result.exitCode !== 0 && !result.killed;
|
|
234
|
+
});
|
|
235
|
+
if (failedCommandNames.length) {
|
|
236
|
+
throw new VirmatorNoTraceError(`${failedCommandNames.join(', ')} failed.`);
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
if (!args.virmatorFlags['--no-configs']) {
|
|
241
|
+
await copyPluginConfigs(args.usedCommands, resolvedConfigs, packageType, monoRepoPackages, log, filteredArgs);
|
|
242
|
+
}
|
|
243
|
+
if (!args.virmatorFlags['--no-deps']) {
|
|
244
|
+
await installPluginNpmDeps({
|
|
245
|
+
cwdPackageJson,
|
|
246
|
+
cwdPackagePath,
|
|
247
|
+
log,
|
|
248
|
+
packageType,
|
|
249
|
+
pluginName: plugin.name,
|
|
250
|
+
pluginPackagePath,
|
|
251
|
+
usedCommands: args.usedCommands,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
const result = await wrapInTry(() => plugin.executor(executorParams));
|
|
255
|
+
if (result instanceof Error) {
|
|
256
|
+
if (result instanceof VirmatorNoTraceError && hideNoTraceTraces) {
|
|
257
|
+
if (result.message) {
|
|
258
|
+
log.error(result.message);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
log.error(result);
|
|
263
|
+
}
|
|
264
|
+
throw new VirmatorNoTraceError(`${args.commands[0]} failed.`);
|
|
265
|
+
}
|
|
266
|
+
if (!result) {
|
|
267
|
+
log.success(`${args.commands[0]} finished.`);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { PackageJson } from 'type-fest';
|
|
2
|
+
import { PackageType } from '../plugin/plugin-env';
|
|
3
|
+
import { UsedVirmatorPluginCommands } from '../plugin/plugin-executor';
|
|
4
|
+
import { PluginNpmDeps } from '../plugin/plugin-init';
|
|
5
|
+
import { PluginLogger } from '../plugin/plugin-logger';
|
|
6
|
+
/** Install's a virmator plugin's listed npm deps depending on which command is being run. */
|
|
7
|
+
export declare function installPluginNpmDeps({ usedCommands, ...params }: {
|
|
8
|
+
cwdPackagePath: string;
|
|
9
|
+
cwdPackageJson: PackageJson;
|
|
10
|
+
pluginPackagePath: string;
|
|
11
|
+
pluginName: string;
|
|
12
|
+
packageType: PackageType;
|
|
13
|
+
log: PluginLogger;
|
|
14
|
+
usedCommands: Readonly<UsedVirmatorPluginCommands>;
|
|
15
|
+
}): Promise<void>;
|
|
16
|
+
/** Installs a set of virmator plugin npm deps. */
|
|
17
|
+
export declare function installNpmDeps({ cwdPackagePath, cwdPackageJson, pluginPackagePath, pluginName, packageType, log, deps, }: {
|
|
18
|
+
cwdPackagePath: string;
|
|
19
|
+
cwdPackageJson: PackageJson;
|
|
20
|
+
pluginPackagePath: string;
|
|
21
|
+
pluginName: string;
|
|
22
|
+
packageType: PackageType;
|
|
23
|
+
log: PluginLogger;
|
|
24
|
+
deps: Partial<PluginNpmDeps>;
|
|
25
|
+
}): Promise<void>;
|