Skip to content

Commit

Permalink
feat: support cli
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Apr 16, 2020
1 parent f7e0312 commit 622636d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 32 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"license": "MIT",
"publisher": "JayFong",
"main": "./out/extension.js",
"bin": "./out/cli.js",
"scripts": {
"compile": "rimraf out && tsc -p tsconfig.build.json",
"postinstall": "node ./node_modules/vscode/bin/install",
Expand Down Expand Up @@ -75,7 +76,8 @@
"dependencies": {
"change-case": "^3.1.0",
"globby": "^10.0.0",
"vtils": "^2.7.1"
"vtils": "^2.7.1",
"yargs": "^15.3.1"
},
"devDependencies": {
"@types/jest": "^25.2.1",
Expand Down
29 changes: 2 additions & 27 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/IndexGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class IndexGenerator {
}
}

private static findMarkers(
public static findMarkers(
text: string,
onInvalid?: (msg: string) => any,
): Marker[] {
Expand Down Expand Up @@ -140,11 +140,11 @@ export class IndexGenerator {
return markers
}

private static normalizePath(path: string): string {
public static normalizePath(path: string): string {
return path.replace(/[/\\]+/g, '/')
}

private static getRelativePath(from: string, to: string): string {
public static getRelativePath(from: string, to: string): string {
const relativePath = IndexGenerator.normalizePath(relative(from, to))
return relativePath.startsWith('../') ? relativePath : `./${relativePath}`
}
Expand Down
35 changes: 35 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env node
import globby from 'globby'
import yargs from 'yargs'
import { generateIndex } from './generateIndex'
import { IndexGenerator } from './IndexGenerator'

const argv = yargs
.usage('Usage: $0 <pattern> [<pattern> ...] [options]')
.help('help', 'Show help')
.alias('help', 'h')
.option('cwd', {
type: 'string',
describe: 'Current working directory',
default: process.cwd(),
})
// Show help if no args
// ref: https://github.com/yargs/yargs/issues/895
.demandCommand(1, '').argv

const filePaths = !argv._.length
? []
: globby.sync(argv._, {
absolute: true,
cwd: argv.cwd,
dot: true,
onlyFiles: true,
unique: true,
gitignore: false,
})

for (const filePath of filePaths) {
generateIndex(filePath, true).then(() => {
console.log(`✔️ ${IndexGenerator.getRelativePath(argv.cwd, filePath)}`)
})
}
3 changes: 2 additions & 1 deletion src/generateIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IndexGenerator } from './IndexGenerator'
export async function generateIndex(
filePath: string,
replaceFile = false,
onWarning?: (msg: string) => any,
): Promise<string> {
if (!existsSync(filePath)) {
throw new Error('File not found.')
Expand All @@ -13,7 +14,7 @@ export async function generateIndex(
filePath: filePath,
fileContent: fileContent,
onWarning: msg => {
console.warn(msg)
onWarning?.(msg)
},
onGenerate: ({ code, marker }) => {
fileContent =
Expand Down

0 comments on commit 622636d

Please sign in to comment.