Skip to content

Commit 74b085f

Browse files
committed
β˜• chore: improve build workflow
1 parent 2b24983 commit 74b085f

File tree

4 files changed

+68
-54
lines changed

4 files changed

+68
-54
lines changed

β€Žscripts/forklift-runner.jsβ€Ž

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@
1616
* @license MIT
1717
*/
1818

19-
const config = {
20-
entry: 'src/index.ts',
21-
outDir: 'dist',
22-
formats: ['esm', 'cjs'],
23-
clean: process.argv.includes('--clean'),
24-
watch: process.argv.includes('--watch'),
25-
verbose: process.argv.includes('--verbose') || process.argv.includes('-v'),
26-
dryRun: process.argv.includes('--dry-run') || process.argv.includes('-n'),
27-
};
19+
// Import and run the forklift module
20+
import('./forklift.js').then(async (forklift) => {
21+
// Merge default configuration with runtime overrides
22+
const config = {
23+
...forklift.defaultConfig,
24+
clean: process.argv.includes('--clean'),
25+
watch: process.argv.includes('--watch'),
26+
verbose: process.argv.includes('--verbose') || process.argv.includes('-v'),
27+
dryRun: process.argv.includes('--dry-run') || process.argv.includes('-n'),
28+
};
2829

29-
if (process.argv.includes('--help') || process.argv.includes('-h')) {
30-
console.log(`
30+
if (process.argv.includes('--help') || process.argv.includes('-h')) {
31+
console.log(`
3132
πŸš€ @wgtechlabs/forklift - Dual Build System
3233
3334
Usage: node scripts/forklift-runner.js [options]
@@ -39,11 +40,9 @@ Options:
3940
--dry-run, -n Show what would be done without building
4041
--help, -h Show this help message
4142
`);
42-
process.exit(0);
43-
}
43+
process.exit(0);
44+
}
4445

45-
// Import and run the forklift module
46-
import('./forklift.js').then(async (forklift) => {
4746
try {
4847
forklift.validateConfig(config);
4948

β€Žscripts/forklift.jsβ€Ž

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,7 @@ function validateCommand(command, args = []) {
195195
*/
196196
function parseArgs() {
197197
const args = process.argv.slice(2);
198-
const config = {
199-
entry: 'src/index.ts',
200-
outDir: 'dist',
201-
formats: ['esm', 'cjs'],
202-
clean: false,
203-
watch: false,
204-
verbose: false,
205-
dryRun: false,
206-
};
198+
const config = { ...defaultConfig };
207199

208200
for (let i = 0; i < args.length; i++) {
209201
const arg = args[i];
@@ -466,41 +458,40 @@ async function cleanOutputDir(outDir, config) {
466458
/**
467459
* Create TypeScript configuration for a specific module format
468460
*
469-
* Generates appropriate compiler options for either ESM or CommonJS output,
470-
* including correct module settings, output directories, and build options.
461+
* Reads the existing tsconfig files and creates a temporary configuration
462+
* with the appropriate output directory for the build process. This approach
463+
* centralizes configuration in the dedicated tsconfig files rather than
464+
* duplicating compiler options.
471465
*
472466
* @param {string} format - Target format ('esm' or 'cjs')
473467
* @param {Object} config - Build configuration
474468
* @param {string} config.outDir - Base output directory
475-
* @returns {Object} TypeScript configuration object
469+
* @returns {Promise<Object>} TypeScript configuration object
476470
*/
477-
function createTsConfig(format, config) {
471+
async function createTsConfig(format, config) {
478472
// Validate paths to prevent traversal
479473
const validatedOutDir = validatePath(config.outDir);
480474

481-
const baseConfig = {
482-
compilerOptions: {
483-
target: 'ES2020',
484-
lib: ['ES2020'],
485-
module: format === 'esm' ? 'ESNext' : 'CommonJS',
486-
moduleResolution: 'node',
487-
declaration: true,
488-
declarationMap: true,
489-
sourceMap: true,
490-
outDir: join(validatedOutDir, format),
491-
rootDir: 'src',
492-
strict: true,
493-
esModuleInterop: true,
494-
skipLibCheck: true,
495-
forceConsistentCasingInFileNames: true,
496-
removeComments: false,
497-
preserveConstEnums: true,
498-
},
499-
include: ['src/**/*'],
500-
exclude: ['node_modules', 'dist', '**/*.test.ts', '**/*.spec.ts']
501-
};
502-
503-
return baseConfig;
475+
try {
476+
// Read the appropriate existing config file
477+
const configFileName = format === 'esm' ? 'tsconfig.esm.json' : 'tsconfig.cjs.json';
478+
const configPath = join(process.cwd(), configFileName);
479+
const configContent = await readFile(configPath, 'utf8');
480+
const existingConfig = JSON.parse(configContent);
481+
482+
// Create a new config based on the existing one, only overriding the outDir
483+
const tsConfig = {
484+
...existingConfig,
485+
compilerOptions: {
486+
...existingConfig.compilerOptions,
487+
outDir: join(validatedOutDir, format)
488+
}
489+
};
490+
491+
return tsConfig;
492+
} catch (error) {
493+
throw new Error(`Failed to read TypeScript config for ${format}: ${error.message}`);
494+
}
504495
}
505496

506497
/**
@@ -731,7 +722,7 @@ async function createTempTsConfig(format, config) {
731722
const TEMP_CONFIG_CJS = 'tsconfig.cjs.temp.json';
732723

733724
const safeTempFilename = (format === 'esm') ? TEMP_CONFIG_ESM : TEMP_CONFIG_CJS;
734-
const tsConfig = createTsConfig(format, config);
725+
const tsConfig = await createTsConfig(format, config);
735726
const workingDir = process.cwd();
736727
const tempFilePath = join(workingDir, safeTempFilename);
737728

@@ -1180,4 +1171,20 @@ async function fixCjsRequires(cjsDir, config) {
11801171
return { filesFixed, requiresFixed };
11811172
}
11821173

1174+
/**
1175+
* Default configuration object for Forklift builds
1176+
*
1177+
* This serves as the single source of truth for default configuration values,
1178+
* ensuring consistency between the main build module and the runner.
1179+
*/
1180+
export const defaultConfig = {
1181+
entry: 'src/index.ts',
1182+
outDir: 'dist',
1183+
formats: ['esm', 'cjs'],
1184+
clean: false,
1185+
watch: false,
1186+
verbose: false,
1187+
dryRun: false,
1188+
};
1189+
11831190
export { build, watchMode, validateConfig, fixEsmImports, createCjsPackageMarker };

β€Žtsconfig.cjs.jsonβ€Ž

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
"compilerOptions": {
44
"module": "CommonJS",
55
"target": "ES2020",
6+
"lib": ["ES2020"],
67
"moduleResolution": "node",
78
"outDir": "./dist/cjs",
9+
"rootDir": "src",
810
"declaration": true,
911
"declarationMap": true,
10-
"sourceMap": true
12+
"sourceMap": true,
13+
"removeComments": false,
14+
"preserveConstEnums": true
1115
},
1216
"include": ["src/**/*"],
1317
"exclude": ["node_modules", "dist", "**/*.test.ts", "**/*.spec.ts"]

β€Žtsconfig.esm.jsonβ€Ž

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
"compilerOptions": {
44
"module": "ESNext",
55
"target": "ES2020",
6+
"lib": ["ES2020"],
67
"moduleResolution": "node",
78
"outDir": "./dist/esm",
9+
"rootDir": "src",
810
"declaration": true,
911
"declarationMap": true,
10-
"sourceMap": true
12+
"sourceMap": true,
13+
"removeComments": false,
14+
"preserveConstEnums": true
1115
},
1216
"include": ["src/**/*"],
1317
"exclude": ["node_modules", "dist", "**/*.test.ts", "**/*.spec.ts"]

0 commit comments

Comments
Β (0)