@komaci/cli 0.0.1

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/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # `@komaci/cli`
2
+
3
+ CLI tool to test out functionality for komaci. Currently supports only one command, `modgen`.
4
+
5
+ ## `modgen` Command
6
+
7
+ This command will generate a komaci module, similar to what [komaci-tester](https://github.com/salesforce/komaci-tester) did, but with some improvements. Some example components can be found in `./sandbox`.
8
+
9
+ One important difference is that the component's namespace is no longer hardcoded to "c". It will now be derived from the module structure. For instance;
10
+
11
+ ```text
12
+ sandbox
13
+ ├── jsconfig.json
14
+ └── modules // LWC 'modules' directory; not required
15
+ ├── c // "c" is the namespace for the modules within this directory
16
+ │ └── komaciAction // "lightning/komaciAction" component — komaciAction in the "c" namespace
17
+ └── lightning // "lightning" is the namespace for the modules below
18
+ ├── komaciAction // "lightning/komaciAction" component — Same as c/komaciAction, but in a trusted namespace
19
+ └── recordForm // "lightning/recordForm" component — copied from ui-lightning-components
20
+ ```
21
+
22
+ ### Usage
23
+
24
+ Run the following from the `packages/@komaci/cli` directory:
25
+
26
+ ```sh
27
+ yarn
28
+ yarn build
29
+ # Run modgen on the component defined @ <path>
30
+ yarn komaci modgen <path>
31
+ ```
32
+
33
+ ### Examples
34
+
35
+ ```sh
36
+ # Run modgen on c/komaciAction:
37
+ yarn komaci modgen sandbox/modules/c/komaciAction
38
+ # Print only the lwc metadata for c/komaciAction:
39
+ yarn komaci modgen sandbox/modules/c/komaciAction --only lwc
40
+ ```
41
+
42
+ Modgen can also take in a list of paths:
43
+
44
+ ```sh
45
+ yarn komaci modgen sandbox/modules/lightning/komaciAction sandbox/modules/lightning/recordForm
46
+ ```
47
+
48
+ If all the modules are in the same subdirectory, you can use a glob pattern:
49
+
50
+ ```sh
51
+ # Run modgen on all modules in the lightning namespace:
52
+ yarn komaci modgen sandbox/modules/lightning/*
53
+ # Run modgen on all modules in all namespaces:
54
+ yarn komaci modgen sandbox/modules/*/*
55
+ ```
package/bin/komaci.js ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ import('../build/index.js');
@@ -0,0 +1,3 @@
1
+ import modgen from './modgen/index.js';
2
+ export { modgen };
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,3 @@
1
+ import modgen from './modgen/index.js';
2
+ export { modgen };
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,6 @@
1
+ import type { Command } from '../../types';
2
+ import type { ModGenOptions } from './types';
3
+ declare const _default: Command<ModGenOptions>;
4
+ export default _default;
5
+ export * from './types.js';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,42 @@
1
+ import { run } from './lib/run.js';
2
+ export default {
3
+ name: 'modgen',
4
+ alias: 'm',
5
+ options: [
6
+ {
7
+ name: 'paths',
8
+ alias: 'p',
9
+ multiple: true,
10
+ type: String,
11
+ required: true,
12
+ defaultOption: true,
13
+ },
14
+ {
15
+ name: 'only',
16
+ alias: 'o',
17
+ type: String,
18
+ multiple: true,
19
+ defaultValue: ['mod', 'lwc', 'doc'],
20
+ },
21
+ {
22
+ name: 'disableKomaci',
23
+ alias: 'd',
24
+ type: Boolean,
25
+ },
26
+ {
27
+ name: 'bundleType',
28
+ alias: 't',
29
+ type: String,
30
+ defaultValue: 'internal',
31
+ },
32
+ {
33
+ name: 'json',
34
+ alias: 'j',
35
+ type: Boolean,
36
+ defaultValue: false,
37
+ },
38
+ ],
39
+ run,
40
+ };
41
+ export * from './types.js';
42
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,26 @@
1
+ import { BundleConfig } from '@lwc-platform/lwc-metadata-next';
2
+ import { ParsedBundle } from '../types';
3
+ /**
4
+ * typeof BundleConfig.file
5
+ */
6
+ declare type BundleFile = {
7
+ fileName: string;
8
+ source: string;
9
+ };
10
+ /**
11
+ * Finds all .js, .mjs, .css, and .html files within the given directory, reads their contents, and combines them into a bundle,
12
+ * and returning the array of BundleFile containing the file's fileName and source
13
+ * @param path
14
+ * @returns An array of BundleFile
15
+ */
16
+ export declare function readBundle(path: string): BundleFile[];
17
+ /**
18
+ * Takes the directory path of an LWC module bundle and an optional set of config overrides. Returns the ParsedBundle, inlcuding
19
+ * the bundle's 1) LWC Metadata and 2) Komaci / Resolvable module.
20
+ * @param path
21
+ * @param configOverrides
22
+ * @returns ParsedBundle that includes the BundleConfig & GeneratorInput inputs, and BundleMetadata and generated module output
23
+ */
24
+ export declare function readAndParseBundle(path: string, configOverrides: Partial<BundleConfig>): ParsedBundle;
25
+ export {};
26
+ //# sourceMappingURL=readAndParseBundle.d.ts.map
@@ -0,0 +1,69 @@
1
+ import { readdirSync, existsSync, readFileSync } from 'fs';
2
+ import { basename, dirname, extname, resolve } from 'path';
3
+ import { collectBundleMetadata } from '@lwc-platform/lwc-metadata-next';
4
+ import { generateKomaciModule } from '@komaci/esm-generator';
5
+ /**
6
+ * Finds all .js, .mjs, .css, and .html files within the given directory, reads their contents, and combines them into a bundle,
7
+ * and returning the array of BundleFile containing the file's fileName and source
8
+ * @param path
9
+ * @returns An array of BundleFile
10
+ */
11
+ export function readBundle(path) {
12
+ path = resolve(path);
13
+ if (existsSync(path)) {
14
+ const contents = readdirSync(path);
15
+ const LWC_EXT = ['.js', '.mjs', '.css', '.html'];
16
+ const lwcBundleFiles = contents.filter((filename) => {
17
+ return LWC_EXT.includes(extname(filename));
18
+ });
19
+ return lwcBundleFiles.map((fileName) => ({
20
+ fileName,
21
+ source: readFileSync(resolve(path, fileName), 'utf-8').toString(),
22
+ }));
23
+ }
24
+ else {
25
+ throw new Error(`path doesn't exist: ${path}`);
26
+ }
27
+ }
28
+ /**
29
+ * Takes the directory path of an LWC module bundle and an optional set of config overrides. Returns the ParsedBundle, inlcuding
30
+ * the bundle's 1) LWC Metadata and 2) Komaci / Resolvable module.
31
+ * @param path
32
+ * @param configOverrides
33
+ * @returns ParsedBundle that includes the BundleConfig & GeneratorInput inputs, and BundleMetadata and generated module output
34
+ */
35
+ export function readAndParseBundle(path, configOverrides) {
36
+ const files = readBundle(path);
37
+ const name = basename(path);
38
+ const namespace = basename(dirname(path));
39
+ const bundleConfig = {
40
+ namespace,
41
+ name,
42
+ type: 'internal',
43
+ namespaceMapping: {},
44
+ files,
45
+ enableKomaci: true,
46
+ ...configOverrides,
47
+ };
48
+ const metadata = collectBundleMetadata(bundleConfig);
49
+ // Create input to module generator.
50
+ const srcFileMap = files.reduce((map, { fileName, source }) => ({ ...map, [fileName]: source }), {});
51
+ const inputFiles = Object.fromEntries(metadata.files
52
+ .map(({ fileName, komaciDoc }) => [fileName, komaciDoc])
53
+ .filter(([, komaciDoc]) => !!komaciDoc));
54
+ const generatorInput = {
55
+ moduleInfo: {
56
+ name,
57
+ namespace: bundleConfig.namespace,
58
+ type: 'bundle',
59
+ files: inputFiles,
60
+ },
61
+ srcFileMap,
62
+ };
63
+ const modGenOutput = generateKomaciModule(generatorInput);
64
+ return {
65
+ input: { bundleConfig, generatorInput },
66
+ output: { modGenOutput, metadata },
67
+ };
68
+ }
69
+ //# sourceMappingURL=readAndParseBundle.js.map
@@ -0,0 +1,7 @@
1
+ import type { ModGenOptions } from '../index.js';
2
+ /**
3
+ * Runs the modgen command with the given Command Options.
4
+ * @param cmdOptions
5
+ */
6
+ export declare function run(cmdOptions: ModGenOptions): void;
7
+ //# sourceMappingURL=run.d.ts.map
@@ -0,0 +1,52 @@
1
+ import { readAndParseBundle } from './readAndParseBundle.js';
2
+ import logger, { serialize } from '../../../lib/log.js';
3
+ const { log, warn } = logger;
4
+ /**
5
+ * Runs the modgen command with the given Command Options.
6
+ * @param cmdOptions
7
+ */
8
+ export function run(cmdOptions) {
9
+ const { paths = [], disableKomaci = false, bundleType: type } = cmdOptions;
10
+ if (paths.length) {
11
+ const parsedBundles = paths.map((path) => readAndParseBundle(path, { enableKomaci: !disableKomaci, type }));
12
+ for (const parsedBundle of parsedBundles) {
13
+ const { input: { bundleConfig = null } = {}, output } = parsedBundle || {};
14
+ const { type, namespace, name, namespaceMapping = {}, enableKomaci = false, files = [], } = bundleConfig || {};
15
+ const fileNames = files.map(({ fileName }) => fileName);
16
+ const toPrint = { type, namespace, name, namespaceMapping, enableKomaci, fileNames, output };
17
+ printOutput(cmdOptions, toPrint);
18
+ }
19
+ }
20
+ else {
21
+ warn('Please provide 1 or more paths to a LWC bundle directory.');
22
+ }
23
+ }
24
+ /**
25
+ * TODO: jsdoc
26
+ * @param cmdOptions
27
+ * @param toPrint
28
+ */
29
+ function printOutput(cmdOptions, toPrint) {
30
+ const { output: { metadata, modGenOutput } = {} } = toPrint;
31
+ const { json, only = [] } = cmdOptions;
32
+ if (only.includes('lwc')) {
33
+ log('===== LWC Metadata =====');
34
+ log(serialize(metadata, !json));
35
+ }
36
+ if (only.includes('doc')) {
37
+ const files = (metadata?.files ?? []);
38
+ const docs = files
39
+ .filter(({ fileType }) => ['js', 'html'].includes(fileType))
40
+ .map((file) => [file.fileName, file.komaciDoc]);
41
+ for (const [fileName, doc] of docs) {
42
+ log('===== Komaci Document =====');
43
+ log(`===== ${fileName} =====`);
44
+ log(serialize(doc, !json));
45
+ }
46
+ }
47
+ if (only.includes('mod')) {
48
+ log('===== Komaci Module =====');
49
+ log(modGenOutput);
50
+ }
51
+ }
52
+ //# sourceMappingURL=run.js.map
@@ -0,0 +1,22 @@
1
+ import { CommandLineOptions } from 'command-line-args';
2
+ import type { BundleType } from '@lwc-platform/lwc-metadata-next/dist/shared/config';
3
+ import { BundleConfig, BundleMetadata } from '@lwc-platform/lwc-metadata-next';
4
+ import { GeneratorInput } from '@komaci/esm-generator';
5
+ export declare type ParsedBundle = {
6
+ input: {
7
+ bundleConfig: BundleConfig;
8
+ generatorInput: GeneratorInput;
9
+ };
10
+ output: {
11
+ metadata: BundleMetadata;
12
+ modGenOutput: string;
13
+ };
14
+ };
15
+ export interface ModGenOptions extends CommandLineOptions {
16
+ paths: string[];
17
+ only: string[];
18
+ disableKomaci: boolean;
19
+ bundleType: BundleType;
20
+ json: boolean;
21
+ }
22
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.d.ts.map
package/build/index.js ADDED
@@ -0,0 +1,18 @@
1
+ import commandLineArgs from 'command-line-args';
2
+ import { modgen } from './cmds/index.js';
3
+ import { commander } from './lib/commander.js';
4
+ import logger from './lib/log.js';
5
+ import chalk from 'chalk';
6
+ const { info, error } = logger;
7
+ const optionDefinitions = [{ name: 'command', defaultOption: true }];
8
+ const mainCommand = commandLineArgs(optionDefinitions, { stopAtFirstUnknown: true });
9
+ const { command, _unknown: argv = [] } = mainCommand;
10
+ info(chalk.cyan(`Command: ${chalk.bgCyan(chalk.black(command))}.`));
11
+ if (!commander(command, argv, [modgen])) {
12
+ error(`No command "${command}" found`);
13
+ process.exit(1);
14
+ }
15
+ else {
16
+ process.exit(0);
17
+ }
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,6 @@
1
+ import { CommandLineOptions } from 'command-line-args';
2
+ import type { Command } from '../types';
3
+ declare type Commands = Command<CommandLineOptions>;
4
+ export declare function commander(commandToRun: string, argv: string[], commands: Commands[]): boolean;
5
+ export {};
6
+ //# sourceMappingURL=commander.d.ts.map
@@ -0,0 +1,14 @@
1
+ import commandLineArgs from 'command-line-args';
2
+ export function commander(commandToRun, argv, commands) {
3
+ const matched = commands.find(({ name, alias }) => {
4
+ return commandToRun === name || commandToRun === alias;
5
+ });
6
+ if (matched) {
7
+ const { options = [], run } = matched;
8
+ const commandOptions = commandLineArgs(options, { argv });
9
+ run(commandOptions);
10
+ return true;
11
+ }
12
+ return false;
13
+ }
14
+ //# sourceMappingURL=commander.js.map
@@ -0,0 +1,23 @@
1
+ declare type FnArg = string | number | boolean | Record<string, unknown> | Array<unknown>;
2
+ declare type Args<T = FnArg> = T;
3
+ /**
4
+ * @param obj to serialize
5
+ * @param [pretty=true] Limits depth to 5 and adds color
6
+ * @returns Seralized version of the object,
7
+ */
8
+ export declare function serialize(obj: unknown, pretty?: boolean): string;
9
+ declare const _default: {
10
+ log: {
11
+ (...data: any[]): void;
12
+ (message?: any, ...optionalParams: any[]): void;
13
+ };
14
+ trace: {
15
+ (...data: any[]): void;
16
+ (message?: any, ...optionalParams: any[]): void;
17
+ };
18
+ error: (...args: Args[]) => void;
19
+ warn: (...args: Args[]) => void;
20
+ info: (...args: Args[]) => void;
21
+ };
22
+ export default _default;
23
+ //# sourceMappingURL=log.d.ts.map
@@ -0,0 +1,27 @@
1
+ import chalk from 'chalk';
2
+ import { inspect } from 'util';
3
+ const { error, log, trace, warn, info } = console;
4
+ const { red, yellow, blue } = chalk;
5
+ /**
6
+ * @param obj to serialize
7
+ * @param [pretty=true] Limits depth to 5 and adds color
8
+ * @returns Seralized version of the object,
9
+ */
10
+ export function serialize(obj, pretty = true) {
11
+ const options = pretty ? { depth: 5, colors: true } : { depth: Infinity };
12
+ return inspect(obj, options);
13
+ }
14
+ export default {
15
+ log,
16
+ trace,
17
+ error: (...args) => {
18
+ return error(...[red('error'), ...args]);
19
+ },
20
+ warn: (...args) => {
21
+ return warn(...[yellow('w⚠rn'), ...args]);
22
+ },
23
+ info: (...args) => {
24
+ return info(...[blue('ℹnfo'), ...args]);
25
+ },
26
+ };
27
+ //# sourceMappingURL=log.js.map
@@ -0,0 +1,8 @@
1
+ import type { CommandLineOptions, OptionDefinition } from 'command-line-args';
2
+ export declare type Command<T extends CommandLineOptions> = {
3
+ name: string;
4
+ alias?: string;
5
+ run: (cmdOptions: T) => void;
6
+ options: OptionDefinition[];
7
+ };
8
+ //# sourceMappingURL=types.d.ts.map
package/build/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@komaci/cli",
3
+ "version": "0.0.1",
4
+ "description": "CLI utility that enables developers to use komaci from the command line.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "types": "build/index.d.ts",
8
+ "main": "build/index.js",
9
+ "module": "build/index.js",
10
+ "files": [
11
+ "build/**/*.js",
12
+ "build/**/*.d.ts"
13
+ ],
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/salesforce/komaci.git",
17
+ "directory": "packages/@komaci/cli"
18
+ },
19
+ "scripts": {
20
+ "build": "tsc -b",
21
+ "komaci": "./packages/@komaci/cli/bin/komaci.js"
22
+ },
23
+ "bugs": {
24
+ "url": "https://github.com/AndrewHuffman/komaci/issues"
25
+ },
26
+ "dependencies": {
27
+ "@komaci/esm-generator": "0.0.1",
28
+ "@lwc-platform/lwc-metadata-next": "^2.18.0-240.2",
29
+ "chalk": "^5.0.1",
30
+ "command-line-args": "^5.2.1"
31
+ },
32
+ "devDependencies": {
33
+ "@types/command-line-args": "^5.2.0",
34
+ "@types/node": "^17.0.22"
35
+ },
36
+ "exports": {
37
+ ".": {
38
+ "import": "./build/index.js"
39
+ }
40
+ },
41
+ "volta": {
42
+ "node": "14.19.1"
43
+ },
44
+ "bin": {
45
+ "komaci": "./bin/komaci.js"
46
+ }
47
+ }