@rnx-kit/cli 0.16.29 → 0.17.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.
Files changed (64) hide show
  1. package/README.md +172 -98
  2. package/bin/rnx-cli.cjs +2 -0
  3. package/lib/align-deps.js +5 -5
  4. package/lib/bin/context.d.ts +25 -0
  5. package/lib/bin/context.js +98 -0
  6. package/lib/bin/externalCommands.d.ts +3 -0
  7. package/lib/bin/externalCommands.js +34 -0
  8. package/lib/bin/rnx-cli.d.ts +2 -0
  9. package/lib/bin/rnx-cli.js +55 -0
  10. package/lib/bundle/cliOptions.d.ts +2 -2
  11. package/lib/bundle/cliOptions.js +27 -27
  12. package/lib/bundle/hermes.d.ts +2 -1
  13. package/lib/bundle/hermes.js +9 -15
  14. package/lib/bundle/metro.js +6 -9
  15. package/lib/bundle.d.ts +3 -3
  16. package/lib/bundle.js +4 -4
  17. package/lib/clean.d.ts +3 -3
  18. package/lib/clean.js +47 -67
  19. package/lib/copy-assets.d.ts +5 -3
  20. package/lib/copy-assets.js +31 -28
  21. package/lib/helpers/externals.d.ts +4 -0
  22. package/lib/{serve/external.js → helpers/externals.js} +10 -8
  23. package/lib/helpers/filesystem.d.ts +3 -0
  24. package/lib/helpers/filesystem.js +31 -0
  25. package/lib/{metro-config.js → helpers/metro-config.js} +1 -1
  26. package/lib/{parsers.d.ts → helpers/parsers.d.ts} +1 -1
  27. package/lib/{parsers.js → helpers/parsers.js} +17 -15
  28. package/lib/index.d.ts +2 -188
  29. package/lib/index.js +1 -5
  30. package/lib/ram-bundle.d.ts +4 -4
  31. package/lib/ram-bundle.js +2 -2
  32. package/lib/serve/keyboard.js +3 -6
  33. package/lib/serve/types.d.ts +2 -2
  34. package/lib/start.d.ts +1 -1
  35. package/lib/start.js +34 -34
  36. package/lib/test.d.ts +2 -1
  37. package/lib/test.js +7 -5
  38. package/lib/write-third-party-notices.js +13 -13
  39. package/package.json +24 -23
  40. package/lib/serve/external.d.ts +0 -7
  41. package/src/align-deps.ts +0 -82
  42. package/src/bundle/cliOptions.ts +0 -82
  43. package/src/bundle/defaultPlugins.ts +0 -16
  44. package/src/bundle/hermes.ts +0 -114
  45. package/src/bundle/kit-config.ts +0 -81
  46. package/src/bundle/metro.ts +0 -66
  47. package/src/bundle/overrides.ts +0 -51
  48. package/src/bundle/types.ts +0 -41
  49. package/src/bundle.ts +0 -69
  50. package/src/clean.ts +0 -223
  51. package/src/copy-assets.ts +0 -545
  52. package/src/index.ts +0 -34
  53. package/src/metro-config.ts +0 -208
  54. package/src/parsers.ts +0 -44
  55. package/src/ram-bundle.ts +0 -78
  56. package/src/serve/external.ts +0 -62
  57. package/src/serve/help.ts +0 -59
  58. package/src/serve/keyboard.ts +0 -76
  59. package/src/serve/kit-config.ts +0 -47
  60. package/src/serve/types.ts +0 -87
  61. package/src/start.ts +0 -316
  62. package/src/test.ts +0 -137
  63. package/src/write-third-party-notices.ts +0 -85
  64. /package/lib/{metro-config.d.ts → helpers/metro-config.d.ts} +0 -0
@@ -1,81 +0,0 @@
1
- import {
2
- getBundleConfig,
3
- getKitConfig,
4
- getPlatformBundleConfig,
5
- } from "@rnx-kit/config";
6
- import type { AllPlatforms } from "@rnx-kit/tools-react-native/platform";
7
- import { getDefaultBundlerPlugins } from "./defaultPlugins";
8
- import type { CliPlatformBundleConfig } from "./types";
9
-
10
- /**
11
- * Get the list of target platforms for bundling.
12
- *
13
- * @param overridePlatform Override platform, typically from the command-line. When given, this overrides the list of target platforms.
14
- * @param targetPlatforms Target platforms, typically from the kit configuration.
15
- * @returns Array of target platforms
16
- */
17
- export function getTargetPlatforms(
18
- overridePlatform?: AllPlatforms,
19
- targetPlatforms?: AllPlatforms[]
20
- ): AllPlatforms[] {
21
- if (overridePlatform) {
22
- return [overridePlatform];
23
- }
24
- if (targetPlatforms && targetPlatforms.length > 0) {
25
- return targetPlatforms;
26
- }
27
- throw new Error(
28
- "No target platforms given. Update the rnx-kit configuration to include a target platform, or provide a target platform on the command-line."
29
- );
30
- }
31
-
32
- function getDefaultBundleParameters(platform: string) {
33
- const extension =
34
- platform === "ios" || platform === "macos" ? "jsbundle" : "bundle";
35
-
36
- return {
37
- entryFile: "index.js",
38
- bundleOutput: `index.${platform}.${extension}`,
39
- sourcemapUseAbsolutePath: false,
40
- };
41
- }
42
-
43
- /**
44
- * Get the bundle configuration and target platform(s) from the rnx-kit
45
- * configuration. Use them to create an array of platform-specific
46
- * bundle configurations.
47
- *
48
- * If an id is given, search for the matching bundle definition. Otherwise, use the first bundle definition.
49
- *
50
- * @param id Optional identity of the target bundle definition to return
51
- * @param overridePlatform Override platform, typically from the command-line. When given, this overrides the list of target platforms.
52
- * @returns Array of platform-specific bundle configurations
53
- */
54
- export function getCliPlatformBundleConfigs(
55
- id?: string,
56
- overridePlatform?: AllPlatforms
57
- ): CliPlatformBundleConfig[] {
58
- const kitConfig = getKitConfig();
59
- const maybeBundleConfig = kitConfig
60
- ? getBundleConfig(kitConfig, id)
61
- : undefined;
62
- const bundleConfig = maybeBundleConfig ?? {};
63
-
64
- const platforms = getTargetPlatforms(overridePlatform, bundleConfig.targets);
65
-
66
- return platforms.map<CliPlatformBundleConfig>((platform) => {
67
- const platformBundleConfig = getPlatformBundleConfig(
68
- bundleConfig,
69
- platform
70
- );
71
-
72
- // apply defaults to fill in any required props that are missing
73
-
74
- return {
75
- ...getDefaultBundlerPlugins(),
76
- ...getDefaultBundleParameters(platform),
77
- ...platformBundleConfig,
78
- platform,
79
- };
80
- });
81
- }
@@ -1,66 +0,0 @@
1
- import { info } from "@rnx-kit/console";
2
- import type { BundleArgs as MetroBundleArgs } from "@rnx-kit/metro-service";
3
- import { bundle } from "@rnx-kit/metro-service";
4
- import * as fs from "fs";
5
- import type { ConfigT } from "metro-config";
6
- import * as path from "path";
7
- import { customizeMetroConfig } from "../metro-config";
8
- import type { CliPlatformBundleConfig } from "./types";
9
-
10
- function ensureDir(p: string): void {
11
- fs.mkdirSync(p, { recursive: true, mode: 0o755 });
12
- }
13
-
14
- /**
15
- * Run the Metro bundler.
16
- *
17
- * @param metroConfig Metro configuration
18
- * @param bundleConfig Bundle configuration
19
- * @param dev Choose whether or not this will be a "developer" bundle. The alternative is a "production" bundle.
20
- * When `true`, warnings are enabled, and the bundle is not minified by default.
21
- * Further, optimizations like constant folding are disabled.
22
- * When `false`, warnings are disabled and the bundle is minified by default.
23
- * @param minify Optionally choose whether or not the bundle is minified. When not set, minification is controlled by the `dev` property.
24
- * @param output Output bundle format; defaults to plain JS
25
- */
26
- export async function metroBundle(
27
- metroConfig: ConfigT,
28
- bundleConfig: CliPlatformBundleConfig,
29
- dev: boolean,
30
- minify?: boolean,
31
- output = bundle
32
- ): Promise<void> {
33
- info(`Bundling ${bundleConfig.platform}...`);
34
-
35
- if (!dev && bundleConfig.treeShake) {
36
- if (minify != null) {
37
- if (typeof bundleConfig.treeShake === "object") {
38
- bundleConfig.treeShake.minify = minify;
39
- } else {
40
- bundleConfig.treeShake = { minify };
41
- }
42
- }
43
- } else {
44
- bundleConfig.treeShake = false;
45
- }
46
-
47
- customizeMetroConfig(metroConfig, bundleConfig);
48
-
49
- const metroBundleArgs: MetroBundleArgs = {
50
- ...bundleConfig,
51
- dev,
52
- minify,
53
- };
54
-
55
- // ensure all output directories exist
56
- ensureDir(path.dirname(metroBundleArgs.bundleOutput));
57
- if (metroBundleArgs.sourcemapOutput) {
58
- ensureDir(path.dirname(metroBundleArgs.sourcemapOutput));
59
- }
60
- if (metroBundleArgs.assetsDest) {
61
- ensureDir(metroBundleArgs.assetsDest);
62
- }
63
-
64
- // create the bundle
65
- await output(metroBundleArgs, metroConfig);
66
- }
@@ -1,51 +0,0 @@
1
- import { pickValues } from "@rnx-kit/tools-language/properties";
2
- import type { CliPlatformBundleConfig } from "./types";
3
-
4
- type BundleConfigOverrides = Partial<
5
- Pick<
6
- CliPlatformBundleConfig,
7
- | "entryFile"
8
- | "bundleOutput"
9
- | "bundleEncoding"
10
- | "sourcemapOutput"
11
- | "sourcemapSourcesRoot"
12
- | "sourcemapUseAbsolutePath"
13
- | "assetsDest"
14
- | "treeShake"
15
- | "unstableTransformProfile"
16
- | "indexedRamBundle"
17
- | "hermes"
18
- >
19
- >;
20
-
21
- export const overridableCommonBundleOptions: readonly (keyof BundleConfigOverrides)[] =
22
- [
23
- "assetsDest",
24
- "bundleEncoding",
25
- "bundleOutput",
26
- "entryFile",
27
- "sourcemapOutput",
28
- "sourcemapSourcesRoot",
29
- "sourcemapUseAbsolutePath",
30
- "unstableTransformProfile",
31
- ];
32
-
33
- /**
34
- * Apply overrides, if any, to each rnx-kit bundle configuration. Overrides are applied in-place.
35
- *
36
- * @param overrides Optional overrides to apply
37
- * @param configs Array of platform-specific bundle configurations. This is modified if any overrides are applied.
38
- * @param keys Config keys to pick from {@link overrides}
39
- */
40
- export function applyBundleConfigOverrides(
41
- overrides: BundleConfigOverrides,
42
- configs: CliPlatformBundleConfig[],
43
- keys: (keyof BundleConfigOverrides)[]
44
- ): void {
45
- const overridesToApply = pickValues(overrides, keys);
46
- if (overridesToApply) {
47
- for (const config of configs) {
48
- Object.assign(config, overridesToApply);
49
- }
50
- }
51
- }
@@ -1,41 +0,0 @@
1
- import type { BundleParameters } from "@rnx-kit/config";
2
- import type { BundleArgs } from "@rnx-kit/metro-service";
3
- import type { AllPlatforms } from "@rnx-kit/tools-react-native/platform";
4
- import type { TransformProfile } from "metro-babel-transformer";
5
-
6
- export type CLICommonBundleOptions = {
7
- id?: string;
8
- entryFile?: string;
9
- platform?: AllPlatforms;
10
- dev: boolean;
11
- minify?: boolean;
12
- bundleOutput?: string;
13
- bundleEncoding?: BundleArgs["bundleEncoding"];
14
- maxWorkers?: number;
15
- sourcemapOutput?: string;
16
- sourcemapSourcesRoot?: string;
17
- sourcemapUseAbsolutePath?: boolean;
18
- assetsDest?: string;
19
- unstableTransformProfile?: TransformProfile;
20
- resetCache?: boolean;
21
- config?: string;
22
- };
23
-
24
- export type CliPlatformBundleConfig = BundleParameters &
25
- Required<
26
- Pick<
27
- BundleParameters,
28
- | "entryFile"
29
- | "bundleOutput"
30
- | "sourcemapUseAbsolutePath"
31
- | "treeShake"
32
- | "plugins"
33
- >
34
- > & {
35
- unstableTransformProfile?: TransformProfile;
36
-
37
- /**
38
- * Target platform for the bundle
39
- */
40
- platform: AllPlatforms;
41
- };
package/src/bundle.ts DELETED
@@ -1,69 +0,0 @@
1
- import type { Config as CLIConfig } from "@react-native-community/cli-types";
2
- import { loadMetroConfig } from "@rnx-kit/metro-service";
3
- import { commonBundleCommandOptions } from "./bundle/cliOptions";
4
- import { emitBytecode } from "./bundle/hermes";
5
- import { getCliPlatformBundleConfigs } from "./bundle/kit-config";
6
- import { metroBundle } from "./bundle/metro";
7
- import {
8
- applyBundleConfigOverrides,
9
- overridableCommonBundleOptions,
10
- } from "./bundle/overrides";
11
- import type { CLICommonBundleOptions } from "./bundle/types";
12
- import { asBoolean } from "./parsers";
13
-
14
- type CLIBundleOptions = CLICommonBundleOptions & {
15
- treeShake?: boolean;
16
- };
17
-
18
- export async function rnxBundle(
19
- _argv: string[],
20
- cliConfig: CLIConfig,
21
- cliOptions: CLIBundleOptions
22
- ): Promise<void> {
23
- const metroConfig = await loadMetroConfig(cliConfig, cliOptions);
24
-
25
- const bundleConfigs = getCliPlatformBundleConfigs(
26
- cliOptions.id,
27
- cliOptions.platform
28
- );
29
-
30
- applyBundleConfigOverrides(cliOptions, bundleConfigs, [
31
- ...overridableCommonBundleOptions,
32
- "hermes",
33
- "treeShake",
34
- ]);
35
-
36
- for (const bundleConfig of bundleConfigs) {
37
- await metroBundle(
38
- metroConfig,
39
- bundleConfig,
40
- cliOptions.dev,
41
- cliOptions.minify
42
- );
43
-
44
- const { bundleOutput, hermes, sourcemapOutput } = bundleConfig;
45
- if (hermes) {
46
- emitBytecode(
47
- bundleOutput,
48
- sourcemapOutput,
49
- hermes === true ? {} : hermes
50
- );
51
- }
52
- }
53
- }
54
-
55
- export const rnxBundleCommand = {
56
- name: "rnx-bundle",
57
- description:
58
- "Bundle your rnx-kit package for offline use. See https://aka.ms/rnx-kit.",
59
- func: rnxBundle,
60
- options: [
61
- ...commonBundleCommandOptions,
62
- {
63
- name: "--tree-shake [boolean]",
64
- description:
65
- "Enable tree shaking to remove unused code and reduce the bundle size.",
66
- parse: asBoolean,
67
- },
68
- ],
69
- };
package/src/clean.ts DELETED
@@ -1,223 +0,0 @@
1
- import type { Config as CLIConfig } from "@react-native-community/cli-types";
2
- import { spawn } from "child_process";
3
- import * as fs from "fs-extra";
4
- import ora from "ora";
5
- import * as os from "os";
6
- import * as path from "path";
7
- import { asResolvedPath } from "./parsers";
8
- import { requireExternal } from "./serve/external";
9
-
10
- type Args = {
11
- include?: string;
12
- projectRoot?: string;
13
- verify?: boolean;
14
- };
15
-
16
- type Task = {
17
- label: string;
18
- action: () => Promise<void>;
19
- };
20
-
21
- type CLICommand = Record<string, Task[]>;
22
-
23
- export async function rnxClean(
24
- _argv: string[],
25
- _config: CLIConfig,
26
- cliOptions: Args
27
- ): Promise<void> {
28
- const projectRoot = cliOptions.projectRoot ?? process.cwd();
29
- if (!fs.existsSync(projectRoot)) {
30
- throw new Error(`Invalid path provided! ${projectRoot}`);
31
- }
32
-
33
- const spinner = ora();
34
- try {
35
- requireExternal("@react-native-community/cli-clean");
36
- spinner.warn(
37
- "`rnx-clean` has been upstreamed to `@react-native-community/cli`. Please use `npx react-native clean` instead."
38
- );
39
- } catch (_) {
40
- // Ignore
41
- }
42
-
43
- const npm = os.platform() === "win32" ? "npm.cmd" : "npm";
44
- const yarn = os.platform() === "win32" ? "yarn.cmd" : "yarn";
45
-
46
- const COMMANDS: CLICommand = {
47
- android: [
48
- {
49
- label: "Clean Gradle cache",
50
- action: () => {
51
- const candidates =
52
- os.platform() === "win32"
53
- ? ["android/gradlew.bat", "gradlew.bat"]
54
- : ["android/gradlew", "gradlew"];
55
- const gradlew = findPath(projectRoot, candidates);
56
- if (gradlew) {
57
- const script = path.basename(gradlew);
58
- return execute(
59
- os.platform() === "win32" ? script : `./${script}`,
60
- ["clean"],
61
- path.dirname(gradlew)
62
- );
63
- } else {
64
- return Promise.resolve();
65
- }
66
- },
67
- },
68
- ],
69
- cocoapods: [
70
- {
71
- label: "Clean CocoaPods cache",
72
- action: () => execute("pod", ["cache", "clean", "--all"], projectRoot),
73
- },
74
- ],
75
- metro: [
76
- {
77
- label: "Clean Metro cache",
78
- action: () => cleanDir(`${os.tmpdir()}/metro-*`),
79
- },
80
- {
81
- label: "Clean Haste cache",
82
- action: () => cleanDir(`${os.tmpdir()}/haste-map-*`),
83
- },
84
- {
85
- label: "Clean React Native cache",
86
- action: () => cleanDir(`${os.tmpdir()}/react-*`),
87
- },
88
- ],
89
- npm: [
90
- {
91
- label: "Remove node_modules",
92
- action: () => cleanDir(`${projectRoot}/node_modules`),
93
- },
94
- ...(cliOptions.verify
95
- ? [
96
- {
97
- label: "Verify npm cache",
98
- action: () => execute(npm, ["cache", "verify"], projectRoot),
99
- },
100
- ]
101
- : []),
102
- ],
103
- watchman: [
104
- {
105
- label: "Stop Watchman",
106
- action: () =>
107
- execute(
108
- os.platform() === "win32" ? "tskill" : "killall",
109
- ["watchman"],
110
- projectRoot
111
- ),
112
- },
113
- {
114
- label: "Delete Watchman cache",
115
- action: () => execute("watchman", ["watch-del-all"], projectRoot),
116
- },
117
- ],
118
- yarn: [
119
- {
120
- label: "Clean Yarn cache",
121
- action: () => execute(yarn, ["cache", "clean"], projectRoot),
122
- },
123
- ],
124
- };
125
-
126
- const categories = cliOptions.include?.split(",") ?? [
127
- "metro",
128
- "npm",
129
- "watchman",
130
- "yarn",
131
- ];
132
-
133
- for (const category of categories) {
134
- const commands = COMMANDS[category];
135
- if (!commands) {
136
- spinner.warn(`Unknown category: ${category}`);
137
- return;
138
- }
139
-
140
- for (const { action, label } of commands) {
141
- spinner.start(label);
142
- await action()
143
- .then(() => {
144
- spinner.succeed();
145
- })
146
- .catch((e) => {
147
- spinner.fail(`${label} » ${e}`);
148
- });
149
- }
150
- }
151
- }
152
-
153
- function cleanDir(path: string): Promise<void> {
154
- if (!fs.existsSync(path)) {
155
- return Promise.resolve();
156
- }
157
-
158
- return fs.rmdir(path, { maxRetries: 3, recursive: true });
159
- }
160
-
161
- function findPath(startPath: string, files: string[]): string | undefined {
162
- // TODO: Find project files via `@react-native-community/cli`
163
- for (const file of files) {
164
- const filename = path.resolve(startPath, file);
165
- if (fs.existsSync(filename)) {
166
- return filename;
167
- }
168
- }
169
-
170
- return undefined;
171
- }
172
-
173
- function execute(command: string, args: string[], cwd: string): Promise<void> {
174
- return new Promise((resolve, reject) => {
175
- const process = spawn(command, args, {
176
- cwd,
177
- stdio: ["inherit", null, null],
178
- shell: command.endsWith(".bat") || command.endsWith(".cmd"),
179
- });
180
-
181
- const stderr: Buffer[] = [];
182
- process.stderr.on("data", (data) => {
183
- stderr.push(data);
184
- });
185
-
186
- process.on("close", (code, signal) => {
187
- if (code === 0) {
188
- resolve();
189
- } else if (stderr) {
190
- reject(Buffer.concat(stderr).toString().trimEnd());
191
- } else if (signal) {
192
- reject(`Failed with signal ${signal}`);
193
- } else {
194
- reject(`Failed with exit code ${code}`);
195
- }
196
- });
197
- });
198
- }
199
-
200
- export const rnxCleanCommand = {
201
- name: "rnx-clean",
202
- func: rnxClean,
203
- description: "Clears React Native project related caches",
204
- options: [
205
- {
206
- name: "--include [android,cocoapods,metro,npm,watchman,yarn]",
207
- description:
208
- "Comma-separated flag of caches to clear, e.g. `npm,yarn`. When not specified, only non-platform specific caches are cleared.",
209
- default: "metro,npm,watchman,yarn",
210
- },
211
- {
212
- name: "--project-root <path>",
213
- description: "Root path to your React Native project",
214
- default: process.cwd(),
215
- parse: asResolvedPath,
216
- },
217
- {
218
- name: "--verify",
219
- description: "Whether to verify the integrity of the cache",
220
- default: false,
221
- },
222
- ],
223
- };