This repository has been archived by the owner on Aug 7, 2020. It is now read-only.
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 nd-02110114/use-yargs
[WIP] Use yargs
- Loading branch information
Showing
11 changed files
with
476 additions
and
107 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
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,22 @@ | ||
import * as yargs from "yargs"; | ||
// @ts-ignore | ||
import * as pkg from "../package.json"; | ||
|
||
export type CLIOptions = { | ||
input: string | ||
} | ||
|
||
const args = yargs | ||
.version(pkg.version) | ||
.option("input", { | ||
alias: "i", | ||
demandOption: true, | ||
describe: "Set the directory path you want to convert", | ||
type: "string", | ||
}) | ||
.help("help") | ||
.detectLocale(false) | ||
|
||
const { _: [target], ...flags } = args.argv; | ||
|
||
export { flags }; |
File renamed without changes.
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,56 @@ | ||
#!/usr/bin/env node | ||
import * as fs from 'fs' | ||
import * as path from 'path' | ||
import * as babel from '@babel/core' | ||
import * as prettier from 'prettier' | ||
import * as chalk from 'chalk' | ||
import * as babelConfig from './config/.babelrc.js' | ||
import * as prettierDefaultConfig from './config/.prettierrc.js' | ||
import { walk, isJsxFile, deleteAtFlow } from './utils' | ||
import { flags } from './args' | ||
|
||
const fsThrowError = (err: NodeJS.ErrnoException) => { | ||
if(err) throw err | ||
} | ||
|
||
// convert (babel, prettier, rename) | ||
const converter = async (fullPath: string): Promise<void> => { | ||
try { | ||
const isJsFile = fullPath.match(/(js|jsx)$/) | ||
if (isJsFile == null) { | ||
return | ||
} | ||
|
||
const config = { cwd: path.join(__dirname, '../'), ...babelConfig } | ||
// flow code is compiled by babel | ||
const result = await babel.transformFileAsync(fullPath, config) | ||
const babeledCode = result.code | ||
const deletedAtFlowCode = deleteAtFlow(babeledCode) | ||
// prettier | ||
// @ts-ignore | ||
const formattedCode = prettier.format(deletedAtFlowCode, prettierDefaultConfig) | ||
fs.writeFile(fullPath, formattedCode, fsThrowError) | ||
// js → ts,tsx | ||
const extension = isJsxFile(formattedCode) ? 'tsx' : 'ts' | ||
const newPath = fullPath.replace(/(js|jsx)$/, '') + extension | ||
fs.rename(fullPath, newPath, fsThrowError) | ||
const cwdPath = path.resolve('.') | ||
console.log(`${path.relative(cwdPath, fullPath)} -> ${path.relative(cwdPath, newPath)}`) | ||
} catch (e) { | ||
throw e | ||
} | ||
} | ||
|
||
// main process | ||
const main = async () => { | ||
const filePath = flags.input | ||
const absPath = path.resolve(filePath) | ||
await walk(absPath, converter) | ||
// @ts-ignore | ||
// console.log(chalk.bold.underline('\n🔥 Finish coverting from flow code to typescript!!')) | ||
} | ||
|
||
main().catch((e: Error) => { | ||
// @ts-ignore | ||
console.error(chalk.red(e.stack)) | ||
}) |
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
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as fs from 'fs' | ||
import * as path from 'path' | ||
|
||
// judge whether the file is a jsx file or not | ||
export const isJsxFile = (code: string): boolean => { | ||
const lines = code.split('\n').filter((line: string) => | ||
line.match(/^\/\//) == null && line.match(/from ("|')react("|')/), | ||
) | ||
if (lines.length > 0) { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// delete @flow & $Flow | ||
export const deleteAtFlow = (code: string): string => { | ||
const lines = code.split('\n') | ||
const newLines = lines.filter((line: string) => line.match(/(@flow|\$Flow)/) == null) | ||
return newLines.join('\n') | ||
} | ||
|
||
// searching files recursively | ||
// ref:https://qiita.com/amay077/items/cc6ee3e66040a5097230 | ||
type fileCallbackType = (path: string) => Promise<void> | ||
|
||
export const walk = async (inputPath: string, fileCallback: fileCallbackType): Promise<void> => { | ||
return await fs.readdir(inputPath, async (err, files) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
await files.forEach(async (f: string) => { | ||
try { | ||
const fullPath = path.join(inputPath, f) | ||
if (fs.statSync(fullPath).isDirectory()) { | ||
// This recursion happens if the path indicates a directory | ||
return await walk(fullPath, fileCallback) | ||
} else { | ||
return await fileCallback(fullPath) | ||
} | ||
} catch(e) { | ||
throw e | ||
} | ||
}) | ||
}) | ||
} | ||
|
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
Oops, something went wrong.