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
feat: Link new and migrate command with the init functionality #66
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -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(); | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this should be the default behaviour for the
new
command? Perhaps we could consider that in a future PR though.