Skip to content

Commit 989b561

Browse files
authored
chore: bundle the CLI in one file for npm users (#848)
1 parent 71b7563 commit 989b561

File tree

9 files changed

+371
-56
lines changed

9 files changed

+371
-56
lines changed

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"version": "0.21.10",
44
"description": "Apify command-line interface (CLI) helps you manage the Apify cloud platform and develop, build, and deploy Apify Actors.",
55
"exports": "./dist/index.js",
6-
"types": "./dist/index.d.ts",
76
"type": "module",
87
"scripts": {
98
"dev:apify": "tsx ./src/entrypoints/apify.ts",
@@ -18,7 +17,7 @@
1817
"format": "biome format . && prettier --check \"**/*.{md,yml,yaml}\"",
1918
"format:fix": "biome format --write . && prettier --write \"**/*.{md,yml,yaml}\"",
2019
"clean": "rimraf dist",
21-
"build": "yarn clean && tsc",
20+
"build": "yarn clean && tsc && tsup",
2221
"build-bundles": "bun run scripts/build-cli-bundles.ts",
2322
"prepack": "yarn insert-cli-metadata && yarn build && yarn update-docs",
2423
"insert-cli-metadata": "tsx scripts/insert-cli-metadata.ts",
@@ -29,9 +28,9 @@
2928
"dist"
3029
],
3130
"bin": {
32-
"actor": "./dist/entrypoints/actor.js",
33-
"apify": "./dist/entrypoints/apify.js",
34-
"apify-cli": "./dist/entrypoints/apify.js"
31+
"actor": "./dist/actor.js",
32+
"apify": "./dist/apify.js",
33+
"apify-cli": "./dist/apify.js"
3534
},
3635
"contributors": [
3736
"Jakub Drobník <[email protected]>",
@@ -145,6 +144,7 @@
145144
"mdast-util-from-markdown": "^2.0.2",
146145
"mock-stdin": "^1.0.0",
147146
"prettier": "^3.5.3",
147+
"tsup": "^8.5.0",
148148
"tsx": "^4.16.5",
149149
"typescript": "^5.8.3",
150150
"typescript-eslint": "^8.31.0",

src/entrypoints/_shared.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import process from 'node:process';
22

33
import chalk from 'chalk';
4+
import { satisfies } from 'semver';
45
// eslint-disable-next-line import/extensions
56
import yargs from 'yargs/yargs';
67

@@ -14,6 +15,7 @@ import {
1415
import type { FlagTag, TaggedFlagBuilder } from '../lib/command-framework/flags.js';
1516
import { renderMainHelpMenu, selectiveRenderHelpForCommand } from '../lib/command-framework/help.js';
1617
import { readStdin } from '../lib/commands/read-stdin.js';
18+
import { SUPPORTED_NODEJS_VERSION } from '../lib/consts.js';
1719
import { useCLIMetadata } from '../lib/hooks/useCLIMetadata.js';
1820
import { shouldSkipVersionCheck } from '../lib/hooks/useCLIVersionCheck.js';
1921
import { useCommandSuggestions } from '../lib/hooks/useCommandSuggestions.js';
@@ -85,6 +87,20 @@ const cliMetadata = useCLIMetadata();
8587

8688
export const USER_AGENT = `Apify CLI/${cliMetadata.version} (https://github.com/apify/apify-cli)`;
8789

90+
export function processVersionCheck(cliName: string) {
91+
if (cliMetadata.installMethod === 'bundle') {
92+
return;
93+
}
94+
95+
if (!satisfies(process.version, SUPPORTED_NODEJS_VERSION)) {
96+
error({
97+
message: `${cliName} CLI requires Node.js version ${SUPPORTED_NODEJS_VERSION}. Your current version is ${process.version}.`,
98+
});
99+
100+
process.exit(1);
101+
}
102+
}
103+
88104
export function printCLIVersionAndExitIfFlagUsed(parsed: Awaited<ReturnType<typeof cli.parse>>) {
89105
if (parsed.v === true || parsed.version === true) {
90106
console.log(cliMetadata.fullVersionString);

src/entrypoints/actor.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
#!/usr/bin/env node
2-
import { satisfies } from 'semver';
3-
42
import { actorCommands } from '../commands/_register.js';
5-
import { SUPPORTED_NODEJS_VERSION } from '../lib/consts.js';
6-
import { error } from '../lib/outputs.js';
7-
import { cli, runCLI } from './_shared.js';
8-
9-
cli.scriptName('actor');
3+
import { cli, processVersionCheck, runCLI } from './_shared.js';
104

11-
if (!satisfies(process.version, SUPPORTED_NODEJS_VERSION)) {
12-
error({
13-
message: `Actor CLI requires Node.js version ${SUPPORTED_NODEJS_VERSION}. Your current version is ${process.version}.`,
14-
});
15-
16-
process.exit(1);
17-
}
5+
processVersionCheck('Actor');
186

197
// Register all commands
208
for (const CommandClass of actorCommands) {

src/entrypoints/apify.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
#!/usr/bin/env node
2-
import { satisfies } from 'semver';
3-
42
import { apifyCommands } from '../commands/_register.js';
5-
import { SUPPORTED_NODEJS_VERSION } from '../lib/consts.js';
6-
import { error } from '../lib/outputs.js';
7-
import { cli, runCLI } from './_shared.js';
8-
9-
if (!satisfies(process.version, SUPPORTED_NODEJS_VERSION)) {
10-
error({
11-
message: `Apify CLI requires Node.js version ${SUPPORTED_NODEJS_VERSION}. Your current version is ${process.version}.`,
12-
});
3+
import { cli, processVersionCheck, runCLI } from './_shared.js';
134

14-
process.exit(1);
15-
}
5+
processVersionCheck('Apify');
166

177
// Register all commands
188
for (const CommandClass of apifyCommands) {

src/lib/consts.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ export const INPUT_FILE_REG_EXP = new RegExp(`^${KEY_VALUE_STORE_KEYS.INPUT}\\..
6060

6161
export const SUPPORTED_NODEJS_VERSION = pkg.engines.node;
6262

63-
export const CURRENT_APIFY_CLI_VERSION = pkg.version;
64-
6563
export const APIFY_CLIENT_DEFAULT_HEADERS = { 'X-Apify-Request-Origin': META_ORIGINS.CLI };
6664

6765
export const MINIMUM_SUPPORTED_PYTHON_VERSION = '3.9.0';

tsconfig.eslint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"features/**/*.ts",
1414
"features/**/*.js",
1515
".github/scripts",
16-
"eslint.config.mjs"
16+
"eslint.config.mjs",
17+
"tsup.config.ts"
1718
]
1819
}

tsconfig.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
"lib": ["ES2022"],
99
"allowJs": true,
1010
"rootDir": "src",
11-
"outDir": "dist",
1211
// For now we need this :(
1312
"skipLibCheck": true,
1413
"tsBuildInfoFile": "dist/.tsbuildinfo",
1514
"noImplicitOverride": true,
1615
"types": ["node"],
17-
"declaration": false,
18-
"declarationMap": false
16+
"noEmit": true
1917
},
2018
"include": ["src"]
2119
}

tsup.config.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// eslint-disable-next-line import/no-extraneous-dependencies
2+
import { defineConfig } from 'tsup';
3+
4+
export default [
5+
defineConfig({
6+
entry: ['src/entrypoints/apify.ts', 'src/entrypoints/actor.ts'],
7+
external: [],
8+
noExternal: [],
9+
platform: 'node',
10+
format: ['esm'],
11+
target: 'es2022',
12+
skipNodeModulesBundle: true,
13+
clean: true,
14+
minify: true,
15+
terserOptions: {
16+
mangle: false,
17+
keep_classnames: true,
18+
keep_fnames: true,
19+
},
20+
splitting: true,
21+
keepNames: true,
22+
dts: false,
23+
sourcemap: true,
24+
bundle: true,
25+
treeshake: false,
26+
outDir: 'dist',
27+
}),
28+
defineConfig({
29+
entry: ['src/index.ts'],
30+
platform: 'node',
31+
format: ['esm'],
32+
target: 'es2022',
33+
sourcemap: false,
34+
dts: false,
35+
outDir: 'dist',
36+
}),
37+
];

0 commit comments

Comments
 (0)