-
-
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.
- Loading branch information
1 parent
f321d80
commit 27b351e
Showing
13 changed files
with
140 additions
and
243 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
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,65 +1,41 @@ | ||
#!/usr/bin/env node | ||
import { convertDirectory } from '#commands/convert-directory' | ||
import { convertFile } from '#commands/convert-file' | ||
import { convertDirectory } from '#commands/convert-directory'; | ||
import { convertFile } from '#commands/convert-file'; | ||
import { | ||
overwriteOptionsDefaultValue, | ||
overwriteOptionsDescription, | ||
overwriteOptionsFlag, | ||
replaceOptionsDefaultValue, | ||
replaceOptionsDescription, | ||
replaceOptionsFlag, | ||
} from '#constants' | ||
import { Command } from 'commander' | ||
replaceOptionsFlag | ||
} from '#constants'; | ||
import { Command } from 'commander'; | ||
|
||
export const cli = new Command() | ||
export const cli = new Command(); | ||
|
||
cli.name('saph-convert') | ||
.description('CLI tool to convert Sapphire.js command files from JS to TS') | ||
.version('1.0.0') | ||
cli.name('saph-convert').description('CLI tool to convert Sapphire.js command files from JS to TS').version('1.0.0'); | ||
|
||
cli.option( | ||
replaceOptionsFlag, | ||
replaceOptionsDescription, | ||
replaceOptionsDefaultValue, | ||
) | ||
cli.option(replaceOptionsFlag, replaceOptionsDescription, replaceOptionsDefaultValue); | ||
|
||
cli.option( | ||
overwriteOptionsFlag, | ||
overwriteOptionsDescription, | ||
overwriteOptionsDefaultValue, | ||
) | ||
cli.option(overwriteOptionsFlag, overwriteOptionsDescription, overwriteOptionsDefaultValue); | ||
|
||
cli.command('convert-file') | ||
.aliases(['cf', 'file', 'f']) | ||
.description('Convert a specific JS command file to TS') | ||
.argument('<inputFile>', 'Path to the JS command file to convert') | ||
.argument( | ||
'[outputPath]', | ||
'Output path for the TS file. Defaults to same directory as input file.', | ||
) | ||
.addHelpText( | ||
'afterAll', | ||
`\nExample:\n $ saph-convert cf src/commands/myCommand.js [dist/commands/myCommand]\n`, | ||
) | ||
.action(convertFile) | ||
.argument('[outputPath]', 'Output path for the TS file. Defaults to same directory as input file.') | ||
.addHelpText('afterAll', `\nExample:\n $ saph-convert cf src/commands/myCommand.js [dist/commands/myCommand]\n`) | ||
.action(convertFile); | ||
|
||
cli.command('convert-directory') | ||
.aliases(['cd', 'directory', 'd']) | ||
.description( | ||
'Recursively convert all JS command files in a directory to TS', | ||
) | ||
.description('Recursively convert all JS command files in a directory to TS') | ||
.argument( | ||
'<directory>', | ||
'Directory containing Sapphire.js JS command files to convert to TS. ❗ Be cautious: this will blindly convert by the `.js` extension in the directory', | ||
) | ||
.argument( | ||
'[outputDirectory]', | ||
'Output directory for the TS files. Defaults to same directory as input.', | ||
) | ||
.addHelpText( | ||
'afterAll', | ||
`\nExample:\n $ saph-convert cdir src/commands [dist/commands]\n`, | ||
'Directory containing Sapphire.js JS command files to convert to TS. ❗ Be cautious: this will blindly convert by the `.js` extension in the directory' | ||
) | ||
.action(convertDirectory) | ||
.argument('[outputDirectory]', 'Output directory for the TS files. Defaults to same directory as input.') | ||
.addHelpText('afterAll', `\nExample:\n $ saph-convert cdir src/commands [dist/commands]\n`) | ||
.action(convertDirectory); | ||
|
||
cli.parse(process.argv) | ||
cli.parse(process.argv); |
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,56 +1,39 @@ | ||
import { | ||
convertToTypeScript, | ||
findJavaScriptFiles, | ||
readJavaScriptFile, | ||
saveTypeScriptFile, | ||
} from '#functions' | ||
import { CommandOptions } from '#lib/types' | ||
import { cli } from '#root/cli' | ||
import path from 'path' | ||
import Logger from '../lib/Logger' | ||
import { convertToTypeScript, findJavaScriptFiles, readJavaScriptFile, saveTypeScriptFile } from '#functions'; | ||
import type { CommandOptions } from '#lib/types'; | ||
import { cli } from '../cli.js'; | ||
import path from 'path'; | ||
import Logger from '../lib/Logger.js'; | ||
|
||
/** | ||
* Recursively converts all JavaScript files in a directory to TypeScript. | ||
* | ||
* @param {string} inputDirectory - The directory containing JavaScript files to convert. | ||
* @param outputDirectory - The output directory for the TypeScript files. | ||
*/ | ||
export const convertDirectory = async ( | ||
inputDirectory: string, | ||
outputDirectory?: string, | ||
): Promise<void> => { | ||
const { overwrite, replace } = cli.opts<CommandOptions>() | ||
export const convertDirectory = async (inputDirectory: string, outputDirectory?: string): Promise<void> => { | ||
const { overwrite, replace } = cli.opts<CommandOptions>(); | ||
try { | ||
const jsFiles = await findJavaScriptFiles(inputDirectory) | ||
const totalFiles = jsFiles.length | ||
const jsFiles = await findJavaScriptFiles(inputDirectory); | ||
const totalFiles = jsFiles.length; | ||
if (totalFiles === 0) { | ||
Logger.error( | ||
`No JavaScript files found in directory ${inputDirectory}.`, | ||
) | ||
return | ||
} else { | ||
Logger.info( | ||
`Converting ${totalFiles} JavaScript files to TypeScript...`, | ||
) | ||
Logger.error(`No JavaScript files found in directory ${inputDirectory}.`); | ||
return; | ||
} | ||
Logger.info(`Converting ${totalFiles} JavaScript files to TypeScript...`); | ||
|
||
for (const jsFile of jsFiles) { | ||
const relativePath = path.relative(inputDirectory, jsFile) | ||
const outputPath = outputDirectory | ||
? path.join( | ||
outputDirectory, | ||
relativePath.replace(/\.js$/, '.ts'), | ||
) | ||
: jsFile.replace(/\.js$/, '.ts') | ||
const jsCode = await readJavaScriptFile(jsFile) | ||
const tsCode = convertToTypeScript(jsCode) | ||
await saveTypeScriptFile(tsCode, outputPath, overwrite, replace) | ||
const relativePath = path.relative(inputDirectory, jsFile); | ||
const outputPath = outputDirectory ? path.join(outputDirectory, relativePath.replace(/\.js$/, '.ts')) : jsFile.replace(/\.js$/, '.ts'); | ||
const jsCode = await readJavaScriptFile(jsFile); | ||
const tsCode = convertToTypeScript(jsCode); | ||
await saveTypeScriptFile(tsCode, outputPath, overwrite, replace); | ||
} | ||
Logger.info(`Completed TS conversion!`) | ||
Logger.info(`Completed TS conversion!`); | ||
} catch (error: unknown) { | ||
if (error instanceof Error) { | ||
Logger.error(`Error: ${error.message}`) | ||
Logger.error(`Error: ${error.message}`); | ||
} else { | ||
Logger.error(`Unexpected error occurred`) | ||
Logger.error(`Unexpected error occurred`); | ||
} | ||
} | ||
} | ||
}; |
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,40 +1,30 @@ | ||
import { | ||
convertToTypeScript, | ||
readJavaScriptFile, | ||
saveTypeScriptFile, | ||
} from '#functions' | ||
import Logger from '#lib/Logger' | ||
import { CommandOptions } from '#lib/types' | ||
import { cli } from '#root/cli' | ||
import path from 'path' | ||
import { convertToTypeScript, readJavaScriptFile, saveTypeScriptFile } from '#functions'; | ||
import Logger from '#lib/Logger'; | ||
import type { CommandOptions } from '#lib/types'; | ||
import { cli } from '../cli.js'; | ||
import path from 'path'; | ||
|
||
/** | ||
* Converts a specific JavaScript file to TypeScript. | ||
* | ||
* @param {string} inputFile - The JavaScript file to convert. | ||
* @param {string} outputPath - The output path for the TypeScript file. | ||
*/ | ||
export const convertFile = async ( | ||
inputFile: string, | ||
outputPath?: string, | ||
): Promise<void> => { | ||
const { overwrite, replace } = cli.opts<CommandOptions>() | ||
export const convertFile = async (inputFile: string, outputPath?: string): Promise<void> => { | ||
const { overwrite, replace } = cli.opts<CommandOptions>(); | ||
try { | ||
const jsCode = await readJavaScriptFile(inputFile) | ||
const tsCode = convertToTypeScript(jsCode) | ||
const jsCode = await readJavaScriptFile(inputFile); | ||
const tsCode = convertToTypeScript(jsCode); | ||
if (!outputPath) { | ||
outputPath = path.join( | ||
path.dirname(inputFile), | ||
path.basename(inputFile, '.js'), | ||
) | ||
outputPath = path.join(path.dirname(inputFile), path.basename(inputFile, '.js')); | ||
} | ||
await saveTypeScriptFile(tsCode, outputPath, overwrite, replace) | ||
Logger.info(`Cmd converted & saved to ${outputPath}`) | ||
await saveTypeScriptFile(tsCode, outputPath, overwrite, replace); | ||
Logger.info(`Cmd converted & saved to ${outputPath}`); | ||
} catch (error: unknown) { | ||
if (error instanceof Error) { | ||
Logger.error(`Error: ${error.message}`) | ||
Logger.error(`Error: ${error.message}`); | ||
} else { | ||
Logger.error(`Unexpected error occurred`) | ||
Logger.error(`Unexpected error occurred`); | ||
} | ||
} | ||
} | ||
}; |
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,9 +1,7 @@ | ||
export const replaceOptionsFlag = '-r, --replace' | ||
export const replaceOptionsDescription = | ||
'Replace original JS command files with converted TypeScript files. Default: Disabled' | ||
export const replaceOptionsDefaultValue = false | ||
export const replaceOptionsFlag = '-r, --replace'; | ||
export const replaceOptionsDescription = 'Replace original JS command files with converted TypeScript files. Default: Disabled'; | ||
export const replaceOptionsDefaultValue = false; | ||
|
||
export const overwriteOptionsFlag = '-o, --overwrite' | ||
export const overwriteOptionsDescription = | ||
'Overwrite existing TypeScript files. Default: Enabled' | ||
export const overwriteOptionsDefaultValue = true | ||
export const overwriteOptionsFlag = '-o, --overwrite'; | ||
export const overwriteOptionsDescription = 'Overwrite existing TypeScript files. Default: Enabled'; | ||
export const overwriteOptionsDefaultValue = true; |
Oops, something went wrong.