Skip to content

Commit

Permalink
feat(template): add write file function
Browse files Browse the repository at this point in the history
  • Loading branch information
taomas committed Feb 26, 2021
1 parent da503c1 commit b53898a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 10 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
"url": "https://github.com/fbi-js/fbi/issues"
},
"dependencies": {
"@types/ejs": "^3.0.6",
"@types/fs-extra": "^9.0.5",
"chalk": "^4.1.0",
"clean-stack": "^3.0.1",
"commander": "6.2.1",
"ejs": "^3.1.6",
"enquirer": "^2.3.6",
"execa": "^5.0.0",
"fs-extra": "^9.1.0",
Expand Down
9 changes: 4 additions & 5 deletions src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export default class CommandCreate extends Command {
flags = [
[
'-p, --package-manager <name>',
'Specifying a package manager. e.g. pnpm/yarn/npm',
'npm'
'Specifying a package manager. e.g. pnpm/yarn/npm'
]
]

Expand Down Expand Up @@ -278,21 +277,21 @@ export default class CommandCreate extends Command {
return selectedTemplate
}

private async getTargetDir(projectName?: string, cwd = process.cwd()) {
private async getTargetDir(projectName?: string) {
const cwd = process.cwd()
if (projectName) {
return {
targetDir: cwd,
subDirectory: projectName
}
}

console.log(`\n${this.style.green('fbi will create a project !')}\n`)

const { subDirectory } = await this.prompt<{ subDirectory: string }>([
{
type: 'input',
name: 'subDirectory',
message: 'Please enter a directory name!',
message: 'Please enter a valid project name!',
initial () {
return 'my-app'
}
Expand Down
74 changes: 73 additions & 1 deletion src/core/template.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as ejs from 'ejs'
import { isAbsolute, join } from 'path'
import { BaseClass } from './base'
import { Factory } from './factory'
Expand Down Expand Up @@ -26,7 +27,6 @@ export abstract class Template extends BaseClass {
// absolute path to template dir
public path = ''
public templates: Template[] = []
protected renderer?: Function
protected data: Record<string | number, any> = {}
protected files: Files = {}
protected targetDir = process.cwd()
Expand Down Expand Up @@ -256,4 +256,76 @@ export abstract class Template extends BaseClass {
// }
// return [...new Set(ret)]
// }

/**
* from -> /factory-web/templates/${template}/src-ts/routes/index.ts.ejs
* to -> ${this.targetDir}/src-ts/routes/index.ts
* @param srcPath file entry path
*/
private getOutputPath(srcPath: string) {
const { template } = this.data.factory
const formatPath = srcPath
.replace(/(.*)(templates)(.*)/, '$3')
.replace(`/${template}`, '')
.replace(/(.*)(.ejs)$/, '$1')
const outputPath = `${this.targetDir}${formatPath}`
return outputPath
}

/**
* copy or render file from srcPath to outputPath, .ejs file will be render by ejs
* @param srcPath file entry path
* @param outputPath file output path
*/
private async writeFile(srcPath: string, outputPath: string) {
const isEjsFile = /(.*)(.ejs)$/.test(srcPath)
if (!isEjsFile) {
this.fs.copy(srcPath, outputPath, {})
} else {
const content = await this.fs.readFile(srcPath, 'utf8')
const rendered = await ejs.render(
content.trim() + '\n',
{
...this.data
},
{
async: true
}
)
await this.fs.outputFile(outputPath, rendered, {})
}

console.log(this.style.grey(`write file: ${outputPath}`))
this.debug('writing file', {
srcPath,
outputPath
})
}

/**
* copy or render files
* @param files file path list
*/
public async writingFiles(files: string[]) {
for (const srcPath of files) {
const isExist = await this.fs.pathExists(srcPath)
const outputPath = this.getOutputPath(srcPath)
const stats = await this.fs.stat(srcPath)
if (isExist) {
if (stats.isFile()) {
try {
await this.writeFile(srcPath, outputPath)
} catch (error) {
this.debug('write file error:', {
srcPath,
outputPath,
error
})
}
} else {
await this.fs.ensureDir(outputPath)
}
}
}
}
}
8 changes: 4 additions & 4 deletions src/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ export function resolveConfig (
packageFile: 'package.json',
packageKey: 'fbi',
organization: 'https://github.com/fbi-js',
packageManager: env.hasNpm
? 'npm'
: env.hasYarn
? 'yarn'
packageManager: env.hasYarn
? 'yarn'
: env.hasNpm
? 'npm'
: env.hasPnpm
? 'pnpm'
: ''
Expand Down

0 comments on commit b53898a

Please sign in to comment.