-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from joris-gallot/feat/cli-command-init
feat: init command
- Loading branch information
Showing
9 changed files
with
156 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,17 @@ | ||
#!/usr/bin/env node | ||
/* eslint-disable no-console */ | ||
import { existsSync } from 'node:fs' | ||
import { readFile } from 'node:fs/promises' | ||
import { resolve } from 'node:path' | ||
import process from 'node:process' | ||
import type { TypestepConfig } from './types.js' | ||
import { tryImport, tscDiagnosticToTscError } from './utils.js' | ||
import { getFinalOutput, parseTscOutput } from './index.js' | ||
|
||
function getTscOutputPath() { | ||
const args = process.argv.slice(2) | ||
|
||
if (args.length === 0) { | ||
console.log('No tsc output file provided') | ||
process.exit(1) | ||
} | ||
|
||
const tscOutputPath = args[0] | ||
|
||
if (!existsSync(tscOutputPath)) { | ||
console.log(`File ${tscOutputPath} does not exist`) | ||
process.exit(1) | ||
} | ||
|
||
return tscOutputPath | ||
} | ||
|
||
async function readConfigFile() { | ||
const configFile = resolve(process.cwd(), 'typestep.config.ts') | ||
|
||
if (!existsSync(configFile)) { | ||
console.log('No config file found') | ||
return | ||
} | ||
|
||
const configModule = await tryImport(configFile) | ||
|
||
if (!configModule?.default) { | ||
console.log('No default export found in config file') | ||
return | ||
} | ||
|
||
return configModule.default as TypestepConfig | ||
} | ||
|
||
async function run() { | ||
const tscOutput = await readFile(getTscOutputPath(), 'utf8') | ||
const configFile = await readConfigFile() | ||
const parsedTscOutput = parseTscOutput(tscOutput) | ||
|
||
const tscDiagnostics = getFinalOutput(parsedTscOutput, configFile) | ||
|
||
if (tscDiagnostics.length === 0) { | ||
console.log('No tsc errors found') | ||
return | ||
} | ||
|
||
console.error(tscDiagnostics.map(tscDiagnosticToTscError).join('\n')) | ||
process.exit(1) | ||
} | ||
|
||
run() | ||
import { defineCommand, runMain } from 'citty' | ||
import { version } from '../package.json' | ||
|
||
const main = defineCommand({ | ||
meta: { | ||
name: 'typestep', | ||
version, | ||
description: 'CLI tool to filter tsc output', | ||
}, | ||
subCommands: { | ||
run: () => import('./commands/run.js').then(mod => mod.default), | ||
init: () => import('./commands/init.js').then(mod => mod.default), | ||
}, | ||
}) | ||
|
||
runMain(main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env node | ||
import { existsSync } from 'node:fs' | ||
import { readFile, writeFile } from 'node:fs/promises' | ||
import { resolve } from 'node:path' | ||
import process from 'node:process' | ||
import { defineCommand } from 'citty' | ||
import { parseTscOutput } from '../index.js' | ||
import { writeTypestepConfig } from '../utils.js' | ||
import { CONFIG_FILE_NAME } from '../constants.js' | ||
|
||
async function init(tscOutputFile: string) { | ||
const tscOutput = await readFile(tscOutputFile, 'utf8') | ||
const parsedTscOutput = parseTscOutput(tscOutput) | ||
|
||
const ignoredFiles = [...new Set(parsedTscOutput.map(({ path }) => path))] | ||
const configFileContent = writeTypestepConfig({ ignoredFiles }) | ||
|
||
return writeFile(CONFIG_FILE_NAME, configFileContent) | ||
} | ||
|
||
export default defineCommand({ | ||
meta: { | ||
name: 'init', | ||
description: 'Initialize the Typestep configuration file with all files from the tsc output marked as ignored', | ||
}, | ||
args: { | ||
tsc_output_file: { | ||
type: 'positional', | ||
description: 'The tsc output file path to be processed', | ||
required: true, | ||
}, | ||
}, | ||
run({ args }) { | ||
if (!existsSync(args.tsc_output_file)) | ||
throw new Error('Tsc output file not found') | ||
|
||
if (existsSync(CONFIG_FILE_NAME)) | ||
throw new Error('Typestep config file already exists') | ||
|
||
init(resolve(process.cwd(), args.tsc_output_file)) | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#!/usr/bin/env node | ||
/* eslint-disable no-console */ | ||
import { existsSync } from 'node:fs' | ||
import { readFile } from 'node:fs/promises' | ||
import { resolve } from 'node:path' | ||
import process from 'node:process' | ||
import { defineCommand } from 'citty' | ||
|
||
import type { TypestepConfig } from '../types.js' | ||
import { tryImport, tscDiagnosticToTscError } from '../utils.js' | ||
import { getFinalOutput, parseTscOutput } from '../index.js' | ||
import { CONFIG_FILE_NAME } from '../constants.js' | ||
|
||
async function readConfigFile() { | ||
const configFile = resolve(process.cwd(), CONFIG_FILE_NAME) | ||
|
||
if (!existsSync(configFile)) { | ||
console.log('No config file found') | ||
return | ||
} | ||
|
||
const configModule = await tryImport(configFile) | ||
|
||
if (!configModule?.default) { | ||
console.log('No default export found in config file') | ||
return | ||
} | ||
|
||
return configModule.default as TypestepConfig | ||
} | ||
|
||
async function run(tscOutputFile: string) { | ||
const tscOutput = await readFile(tscOutputFile, 'utf8') | ||
const configFile = await readConfigFile() | ||
const parsedTscOutput = parseTscOutput(tscOutput) | ||
|
||
const tscDiagnostics = getFinalOutput(parsedTscOutput, configFile) | ||
|
||
if (tscDiagnostics.length === 0) { | ||
console.log('No tsc errors found') | ||
return | ||
} | ||
|
||
console.error(tscDiagnostics.map(tscDiagnosticToTscError).join('\n')) | ||
process.exit(1) | ||
} | ||
|
||
export default defineCommand({ | ||
meta: { | ||
name: 'run', | ||
description: 'Run the Typestep CLI tool with the tsc output file as input', | ||
}, | ||
args: { | ||
tsc_output_file: { | ||
type: 'positional', | ||
description: 'The tsc output file path to be processed', | ||
required: true, | ||
}, | ||
}, | ||
run({ args }) { | ||
if (!existsSync(args.tsc_output_file)) | ||
throw new Error('Tsc output file not found') | ||
|
||
run(resolve(process.cwd(), args.tsc_output_file)) | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const CONFIG_FILE_NAME = 'typestep.config.ts' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters