-
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
718c7dc
commit 6693db6
Showing
8 changed files
with
111 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './interfaces'; | ||
export * from './stage-runner'; |
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,7 @@ | ||
import type { ParamsContext } from '../params'; | ||
|
||
export interface Stage { | ||
label: string; | ||
callback: (context: ParamsContext) => Promise<unknown>; | ||
shouldRun?: (context: ParamsContext) => boolean; | ||
} |
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,43 @@ | ||
import type { ParamsContext } from '../params'; | ||
import type { Stage } from './interfaces'; | ||
import { copyFiles, renderTemplates, installDependencies } from './stages'; | ||
|
||
export class StageRunner { | ||
beforeEach: (stage: Stage) => void = () => null; | ||
afterEach: (stage: Stage) => void = () => null; | ||
|
||
private stages: Stage[] = []; | ||
|
||
constructor(private context: ParamsContext) { | ||
this.initStages(); | ||
} | ||
|
||
async runAll(): Promise<void> { | ||
for (const stage of this.stages) { | ||
const shouldRun = stage.shouldRun || (() => true); | ||
this.beforeEach(stage); | ||
if (shouldRun(this.context)) { | ||
await stage.callback(this.context); | ||
} | ||
this.afterEach(stage); | ||
} | ||
} | ||
|
||
private initStages() { | ||
this.stages = [ | ||
{ | ||
label: 'Copy files', | ||
callback: copyFiles, | ||
}, | ||
{ | ||
label: 'Render templates', | ||
callback: renderTemplates, | ||
}, | ||
{ | ||
label: 'Install dependencies', | ||
callback: installDependencies, | ||
shouldRun: () => this.context.doInstall, | ||
}, | ||
]; | ||
} | ||
} |
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,10 @@ | ||
import * as fse from 'fs-extra'; | ||
import type { ParamsContext } from '../../params'; | ||
|
||
export async function copyFiles(context: ParamsContext): Promise<void> { | ||
const { baseDir, sourceDir, frameworkDir, targetDir } = context; | ||
|
||
await fse.copy(baseDir, targetDir); | ||
await fse.copy(sourceDir, targetDir); | ||
await fse.copy(frameworkDir, targetDir); | ||
} |
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,3 @@ | ||
export * from './copyFiles'; | ||
export * from './renderTemplates'; | ||
export * from './installDependencies'; |
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,8 @@ | ||
import execa from 'execa'; | ||
import type { ParamsContext } from '../../params'; | ||
|
||
export async function installDependencies( | ||
context: ParamsContext, | ||
): Promise<void> { | ||
await execa('yarn', [], { cwd: context.targetDir, stdio: 'inherit' }); | ||
} |
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,23 @@ | ||
import { renderToFolder } from 'template-file'; | ||
import type { ParamsContext } from '../../params'; | ||
|
||
export async function renderTemplates(context: ParamsContext): Promise<void> { | ||
const { appName, author, description, targetDir } = context; | ||
|
||
// TODO: create PR on template-file | ||
await renderToFolder(`${targetDir}/*.*`, targetDir, { | ||
appName, | ||
author, | ||
description, | ||
}); | ||
await renderToFolder(`${targetDir}/LICENSE`, targetDir, { | ||
appName, | ||
author, | ||
description, | ||
}); | ||
await renderToFolder(`${targetDir}/src/*.*`, `${targetDir}/src`, { | ||
appName, | ||
author, | ||
description, | ||
}); | ||
} |