This repository has been archived by the owner on Jan 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor init logic into its own class
- Loading branch information
Jamie Lynch
committed
Feb 1, 2021
1 parent
ddf8150
commit 00384af
Showing
3 changed files
with
95 additions
and
58 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,17 @@ | ||
import { ProjectBuilder } from "./init"; | ||
|
||
describe("The ProjectBuilder class", () => { | ||
describe("validateConfig function", () => { | ||
test("throws an error if the directory is not empty", () => { | ||
expect(() => | ||
ProjectBuilder.validateConfig({ outputDir: "./src" }) | ||
).toThrow(); | ||
}); | ||
|
||
test("does not throw an error if the directory is empty", () => { | ||
expect(() => | ||
ProjectBuilder.validateConfig({ outputDir: "./src/empty" }) | ||
).not.toThrow(); | ||
}); | ||
}); | ||
}); |
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,73 @@ | ||
import { | ||
copyFileSync, | ||
existsSync, | ||
lstatSync, | ||
mkdirSync, | ||
readdirSync, | ||
} from "fs"; | ||
import { join } from "path"; | ||
import type Command from "@oclif/command"; | ||
import { checkDirectoryIsEmpty } from "../utils/args"; | ||
|
||
export interface InitConfig { | ||
outputDir: string; | ||
} | ||
|
||
// TODO: Add yarn or npm flag | ||
// TODO: Add project name flag | ||
export class ProjectBuilder { | ||
templateDir = `${__dirname}/../template`; | ||
config: InitConfig; | ||
command: Command; | ||
|
||
static validateConfig = (config: InitConfig): void => { | ||
checkDirectoryIsEmpty(config.outputDir); | ||
}; | ||
|
||
constructor(config: InitConfig, command: Command) { | ||
this.config = config; | ||
this.command = command; | ||
} | ||
|
||
buildDirectory(): void { | ||
ProjectBuilder.validateConfig(this.config); | ||
|
||
if (!existsSync(this.config.outputDir)) { | ||
this.command.log(`Creating ${this.config.outputDir}`); | ||
mkdirSync(this.config.outputDir); | ||
} | ||
|
||
this.command.log("Copying template files"); | ||
// TODO: Replace any params in files with .template extensions | ||
this.copyFiles(this.templateDir, this.config.outputDir); | ||
|
||
this.command.log("Success!"); | ||
// TODO: Can we do this here? | ||
this.command.log("Run ./script/setup to install dependencies"); | ||
} | ||
|
||
copyFiles(sourcePath: string, targetPath: string): void { | ||
for (const file of readdirSync(sourcePath)) { | ||
const path = join(sourcePath, file); | ||
|
||
if (path.endsWith(".ignore")) { | ||
continue; | ||
} else if (lstatSync(path).isDirectory()) { | ||
const nestedTargetPath = join(targetPath, file); | ||
if (!existsSync(nestedTargetPath)) { | ||
mkdirSync(nestedTargetPath); | ||
} | ||
this.copyFiles(path, nestedTargetPath); | ||
} else if (path.endsWith(".template")) { | ||
copyFileSync(path, join(targetPath, file.replace(".template", ""))); | ||
} else { | ||
copyFileSync(path, join(targetPath, file)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
export const buildDirectory = (config: InitConfig, command: Command): void => { | ||
const builder = new ProjectBuilder(config, command); | ||
builder.buildDirectory(); | ||
}; |