Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor makeDebugCommand #341

Merged
merged 2 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ten-cheetahs-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@atlaspack/cli': patch
---

Minor internal refactor
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