Skip to content

Commit

Permalink
Refactor makeDebugCommand
Browse files Browse the repository at this point in the history
Try to make it easier to add debug commands.

Test Plan: N/A

Pull Request: #341
  • Loading branch information
yamadapc committed Feb 16, 2025
1 parent bfe81e5 commit 5b0a699
Showing 1 changed file with 42 additions and 54 deletions.
96 changes: 42 additions & 54 deletions packages/core/cli/src/makeDebugCommand.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow strict-local

import {NodeFS} from '@atlaspack/fs';
import logger from '@atlaspack/logger';
import commander, {type commander$Command} from 'commander';
import path from 'path';
import {normalizeOptions, type Options} from './normalizeOptions';
Expand All @@ -13,38 +14,50 @@ export function makeDebugCommand(): commander$Command {
const debug = new commander.Command('debug').description(
'Debug commands for atlaspack',
);
const getInstance = async (args, opts, command) => {
let entries = args;

if (entries.length === 0) {
entries = ['.'];
}
entries = entries.map((entry) => path.resolve(entry));

Object.assign(command, opts);
const fs = new NodeFS();
const options = await normalizeOptions(command, fs);

const Atlaspack = require('@atlaspack/core').default;

const atlaspack = new Atlaspack({
entries,
defaultConfig: require.resolve('@atlaspack/config-default', {
paths: [fs.cwd(), __dirname],
}),
shouldPatchConsole: false,
...options,
shouldBuildLazily: true,
watchBackend: 'watchman',
});
logger.info({
message: 'Created atlaspack instance',
origin: '@atlaspack/cli',
});

return atlaspack;
};

const invalidate = debug
.command('invalidate [input...]')
.description('Run cache invalidation, then exit')
.action(async (args: string[], opts: Options, command: CommandExt) => {
try {
let entries = args;
if (entries.length === 0) {
entries = ['.'];
}
entries = entries.map((entry) => path.resolve(entry));
Object.assign(command, opts);

const fs = new NodeFS();
const options = await normalizeOptions(command, fs);

const Atlaspack = require('@atlaspack/core').default;

const atlaspack = new Atlaspack({
entries,
defaultConfig: require.resolve('@atlaspack/config-default', {
paths: [fs.cwd(), __dirname],
}),
shouldPatchConsole: false,
...options,
shouldBuildLazily: true,
watchBackend: 'watchman',
});
console.log('Created atlaspack instance');
const atlaspack = await getInstance(args, opts, command);

await atlaspack.unstable_invalidate();
console.log('Done invalidating cache');
logger.info({
message: 'Done invalidating cache',
origin: '@atlaspack/cli',
});
} catch (err) {
handleUncaughtException(err);
}
Expand All @@ -56,38 +69,13 @@ export function makeDebugCommand(): commander$Command {
.description('Build the asset graph then exit')
.action(async (args: string[], opts: Options, command: CommandExt) => {
try {
let entries = args;

if (entries.length === 0) {
entries = ['.'];
}
entries = entries.map((entry) => path.resolve(entry));

Object.assign(command, opts);
const fs = new NodeFS();
const options = await normalizeOptions(command, fs);

const Atlaspack = require('@atlaspack/core').default;

const atlaspack = new Atlaspack({
entries,
defaultConfig: require.resolve('@atlaspack/config-default', {
paths: [fs.cwd(), __dirname],
}),
shouldPatchConsole: false,
...options,
shouldBuildLazily: true,
watchBackend: 'watchman',
featureFlags: {
...options.featureFlags,
fixQuadraticCacheInvalidation: 'NEW',
useLmdbJsLite: true,
},
});

console.log('Created atlaspack instance');
const atlaspack = await getInstance(args, opts, command);

await atlaspack.unstable_buildAssetGraph();
logger.info({
message: 'Done building asset graph',
origin: '@atlaspack/cli',
});
process.exit(0);
} catch (err) {
handleUncaughtException(err);
Expand Down

0 comments on commit 5b0a699

Please sign in to comment.