-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
8 changed files
with
145 additions
and
48 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
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,22 +1 @@ | ||
import { createPlugin } from '@xus/cli' | ||
import { createTemp } from './create' | ||
|
||
export default createPlugin({ | ||
name: 'cmd:create', | ||
apply(api) { | ||
api.registerCommand( | ||
'create', | ||
{ | ||
desc: 'create template project', | ||
usage: 'xus create [dir]', | ||
options: { | ||
'--template': 'point a template', | ||
'-t': 'point a template short option' | ||
} | ||
}, | ||
async (args) => { | ||
await createTemp(args, api) | ||
} | ||
) | ||
} | ||
}) | ||
export * from './create' |
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,19 @@ | ||
import chalk from 'chalk' | ||
|
||
const INFO = chalk.black.bgBlue(' INFO ') | ||
const WARN = chalk.black.bgHex('#faad14')(' WARN ') | ||
const ERROR = chalk.black.bgRed(' ERROR ') | ||
const SUCCESS = chalk.black.bgGreen(' SUCCESS ') | ||
|
||
export const info = (msg: string) => { | ||
console.log(INFO, msg) | ||
} | ||
export const wran = (msg: string) => { | ||
console.log(WARN, msg) | ||
} | ||
export const error = (msg: string) => { | ||
console.log(ERROR, msg) | ||
} | ||
export const success = (msg: string) => { | ||
console.log(SUCCESS, msg) | ||
} |
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,35 @@ | ||
import ora from 'ora' | ||
import chalk from 'chalk' | ||
import logSymbols from 'log-symbols' | ||
|
||
type OraIns = ora.Ora | null | ||
|
||
export class Spinner { | ||
private oraIns!: OraIns | ||
|
||
start(msg: string) { | ||
if (this.oraIns) { | ||
this.oraIns.stop() | ||
} else { | ||
this.oraIns = ora() | ||
} | ||
this.oraIns.text = chalk.yellow(' ' + msg) | ||
this.oraIns.start() | ||
} | ||
|
||
failed(text?: string) { | ||
this.oraIns && | ||
this.oraIns.stopAndPersist({ | ||
symbol: logSymbols.error, | ||
text: chalk.red(text || this.oraIns.text) | ||
}) | ||
} | ||
|
||
succeed(text?: string) { | ||
this.oraIns && | ||
this.oraIns.stopAndPersist({ | ||
symbol: logSymbols.success, | ||
text: chalk.green(text || this.oraIns.text) | ||
}) | ||
} | ||
} |
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,75 @@ | ||
import execa from 'execa' | ||
import { | ||
mkdirSync, | ||
readdirSync, | ||
statSync, | ||
copyFileSync, | ||
existsSync, | ||
lstatSync, | ||
rmdirSync, | ||
unlinkSync | ||
} from 'fs' | ||
import { resolve } from 'path' | ||
import { error } from './logger' | ||
import { Spinner } from './spinner' | ||
|
||
const spinner = new Spinner() | ||
|
||
export function copy(src: string, dest: string) { | ||
const stat = statSync(src) | ||
if (stat.isDirectory()) { | ||
mkdirSync(dest, { recursive: true }) | ||
for (const file of readdirSync(src)) { | ||
const srcPath = resolve(src, file) | ||
const destPath = resolve(dest, file) | ||
copy(srcPath, destPath) | ||
} | ||
} else { | ||
copyFileSync(src, dest) | ||
} | ||
} | ||
|
||
export function emptyDir(dir: string) { | ||
if (!existsSync(dir)) { | ||
return | ||
} | ||
for (const file of readdirSync(dir)) { | ||
const abs = resolve(dir, file) | ||
if (lstatSync(abs).isDirectory()) { | ||
emptyDir(abs) | ||
rmdirSync(abs) | ||
} else { | ||
unlinkSync(abs) | ||
} | ||
} | ||
} | ||
|
||
export const getPkgManager = () => | ||
/yarn/.test(process.env?.npm_execpath || '') ? 'yarn' : 'npm' | ||
|
||
export type IRunCmdMessage = { | ||
start: string | ||
succeed: string | ||
failed: string | ||
} | ||
|
||
export function runCmd( | ||
cmd: string, | ||
args: string[], | ||
message: IRunCmdMessage, | ||
options?: execa.Options | ||
): Promise<boolean> { | ||
spinner.start(message.start) | ||
return new Promise((resolve, reject) => { | ||
execa(cmd, args, options) | ||
.then(() => { | ||
spinner.succeed(message.succeed) | ||
resolve(true) | ||
}) | ||
.catch((err: any) => { | ||
spinner.failed(message.failed) | ||
error(err) | ||
reject(false) | ||
}) | ||
}) | ||
} |
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