Skip to content

Commit

Permalink
feat: use fs promise instead of sync
Browse files Browse the repository at this point in the history
Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
  • Loading branch information
neilime committed Jan 4, 2024
1 parent 1a4b4b0 commit 7fc348b
Show file tree
Hide file tree
Showing 38 changed files with 715 additions and 484 deletions.
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ updates:
interval: weekly
day: friday
time: "04:00"
groups:
development-dependencies:
dependency-type: development

- package-ecosystem: github-actions
open-pull-requests-limit: 20
directory: "/"
schedule:
interval: weekly
day: friday
time: "04:00"
groups:
github-actions-dependencies:
patterns:
- "*"
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@
},
"devDependencies": {
"@ts-dev-tools/core": "^1.6.1",
"@types/diff": "^5.0.8",
"@types/diff": "^5.0.9",
"@types/figlet": "^1.5.8",
"@types/mock-fs": "^4.13.4",
"@types/parse-github-url": "^1.0.3",
"@types/prompts": "^2.4.8",
"@types/prompts": "^2.4.9",
"@types/shelljs": "^0.8.15",
"@types/tmp": "^0.2.6",
"fs-extra": "^11.1.1",
"fs-extra": "^11.2.0",
"jest-serializer-html": "^7.1.0",
"mock-fs": "^5.2.0",
"mock-spawn": "^0.2.6",
"rimraf": "^5.0.1",
"tmp": "^0.2.1",
"typedoc": "^0.25.3"
"typedoc": "^0.25.6"
},
"eslintConfig": {
"root": true,
Expand Down
125 changes: 67 additions & 58 deletions src/actions/add-hosting/adapters/amplify/Amplify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default class Amplify extends AbstractAdapterWithPackageAction implements
this.consoleService.info("Prepare Amplify configuration...");
const projectBranch = await this.gitService.getGitCurrentBranch(realpath, "master");

let projectName = this.getProjectName(realpath);
let projectName = await this.getProjectName(realpath);
if (!projectName) {
const response = await prompts([
{
Expand Down Expand Up @@ -131,27 +131,26 @@ export default class Amplify extends AbstractAdapterWithPackageAction implements
await this.addHosting(realpath);

const appFilePath = await this.getAppFilePath(realpath);
await this.fileFactory
.fromFile<TypescriptFile>(appFilePath)
.setImports(
[
{
packageName: "@reactionable/amplify",
modules: { IIdentityContextProviderProps: "" },
},
],
[
{
packageName: "@reactionable/core",
modules: { IIdentityContextProviderProps: "" },
},
]
)
.saveFile();
const appFile = await this.fileFactory.fromFile<TypescriptFile>(appFilePath);
appFile.setImports(
[
{
packageName: "@reactionable/amplify",
modules: { IIdentityContextProviderProps: "" },
},
],
[
{
packageName: "@reactionable/core",
modules: { IIdentityContextProviderProps: "" },
},
]
);
await appFile.saveFile();

const indexFilePath = await this.getEntrypointFilePath(realpath);
await this.fileFactory
.fromFile<TypescriptFile>(indexFilePath)
const entrypointFilePath = await this.getEntrypointFilePath(realpath);
const entrypointFile = await this.fileFactory.fromFile<TypescriptFile>(entrypointFilePath);
entrypointFile
.setImports([
{ packageName: "aws-amplify", modules: { Amplify: TypescriptImport.defaultImport } },
{ packageName: "./aws-exports", modules: { awsconfig: TypescriptImport.defaultImport } },
Expand All @@ -160,32 +159,33 @@ export default class Amplify extends AbstractAdapterWithPackageAction implements
modules: { [TypescriptImport.defaultImport]: TypescriptImport.defaultImport },
},
])
.appendContent("Amplify.configure(awsconfig);", "import './index.scss';")
.saveFile();
.appendContent("Amplify.configure(awsconfig);", "import './index.scss';");

await entrypointFile.saveFile();

const i18nFilepath = resolve(
realpath,
await this.getLibDirectoryPath(realpath),
"i18n/i18n.ts"
);

await this.fileFactory
.fromFile<TypescriptFile>(i18nFilepath)
.setImports(
[
{
packageName: "@reactionable/amplify",
modules: { initializeI18n: "" },
},
],
[
{
packageName: "@reactionable/core",
modules: { initializeI18n: "" },
},
]
)
.saveFile();
const i18nFile = await this.fileFactory.fromFile<TypescriptFile>(i18nFilepath);
i18nFile.setImports(
[
{
packageName: "@reactionable/amplify",
modules: { initializeI18n: "" },
},
],
[
{
packageName: "@reactionable/core",
modules: { initializeI18n: "" },
},
]
);

await i18nFile.saveFile();

await this.packageManagerService.installPackages(realpath, ["concurrently"], false, true);
await this.packageManagerService.updatePackageJson(realpath, {
Expand Down Expand Up @@ -242,28 +242,31 @@ export default class Amplify extends AbstractAdapterWithPackageAction implements
return this.cliService.execCmd([cmd, ...args], realpath, silent);
}

private getProjectName(realpath: string): string | undefined {
private async getProjectName(realpath: string): Promise<string | undefined> {
const projectConfigFilePath = resolve(realpath, "amplify/.config/project-config.json");

if (!this.fileService.fileExistsSync(projectConfigFilePath)) {
if (!(await this.fileService.fileExists(projectConfigFilePath))) {
return undefined;
}

return this.fileFactory.fromFile<JsonFile>(projectConfigFilePath).getData<ProjectConfig>()
?.projectName;
const projectConfigFile = await this.fileFactory.fromFile<JsonFile>(projectConfigFilePath);

return projectConfigFile.getData<ProjectConfig>()?.projectName;
}

private getBackendConfig(realpath: string): BackendConfig | undefined {
private async getBackendConfig(realpath: string): Promise<BackendConfig | undefined> {
const backendConfigFilePath = resolve(realpath, "amplify/backend/backend-config.json");
if (!this.fileService.fileExistsSync(backendConfigFilePath)) {
if (!(await this.fileService.fileExists(backendConfigFilePath))) {
return undefined;
}

return this.fileFactory.fromFile<JsonFile>(backendConfigFilePath).getData<BackendConfig>();
const backendConfigFile = await this.fileFactory.fromFile<JsonFile>(backendConfigFilePath);

return backendConfigFile.getData<BackendConfig>();
}

private async addAuth(realpath: string) {
const backendConfig = this.getBackendConfig(realpath);
const backendConfig = await this.getBackendConfig(realpath);
const isAuthAdded = !!backendConfig?.auth;

if (!isAuthAdded) {
Expand All @@ -284,8 +287,10 @@ export default class Amplify extends AbstractAdapterWithPackageAction implements
await this.execAmplifyCmd(["add", "auth"], realpath);
}

await this.fileFactory
.fromFile<TypescriptFile>(await this.getAppFilePath(realpath))
const appFile = await this.fileFactory.fromFile<TypescriptFile>(
await this.getAppFilePath(realpath)
);
appFile
.setImports([
{
packageName: "@reactionable/amplify",
Expand All @@ -295,17 +300,21 @@ export default class Amplify extends AbstractAdapterWithPackageAction implements
},
},
])
.replaceContent(/identity: undefined,.*$/m, "identity: useIdentityContextProviderProps(),")
.saveFile();
.replaceContent(/identity: undefined,.*$/m, "identity: useIdentityContextProviderProps(),");
appFile.saveFile();

await this.fileFactory
.fromFile(await this.getEntrypointFilePath(realpath))
.appendContent("import '@aws-amplify/ui/dist/style.css';", "import './index.scss';")
.saveFile();
const entrypointFile = await this.fileFactory.fromFile(
await this.getEntrypointFilePath(realpath)
);
entrypointFile.appendContent(
"import '@aws-amplify/ui/dist/style.css';",
"import './index.scss';"
);
await entrypointFile.saveFile();
}

private async addApi(realpath: string) {
const backendConfig = this.getBackendConfig(realpath);
const backendConfig = await this.getBackendConfig(realpath);
const isApiAdded = !!backendConfig?.api;

if (isApiAdded) {
Expand All @@ -329,7 +338,7 @@ export default class Amplify extends AbstractAdapterWithPackageAction implements
}

private async addHosting(realpath: string) {
const backendConfig = this.getBackendConfig(realpath);
const backendConfig = await this.getBackendConfig(realpath);
const isHostingAdded = !!backendConfig?.hosting?.amplifyhosting;

if (isHostingAdded) {
Expand Down
23 changes: 11 additions & 12 deletions src/actions/add-hosting/adapters/netlify/Netlify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class Netlify extends AbstractAdapterAction implements HostingAda
}

async isEnabled(realpath: string): Promise<boolean> {
return this.fileService.fileExistsSync(resolve(realpath, "netlify.toml"));
return this.fileService.fileExists(resolve(realpath, "netlify.toml"));
}

async run({ realpath }: AdapterActionOptions): Promise<void> {
Expand All @@ -57,17 +57,16 @@ export default class Netlify extends AbstractAdapterAction implements HostingAda

const netlifyFilePath = resolve(realpath, "netlify.toml");

await this.fileFactory
.fromFile<TomlFile>(netlifyFilePath)
.appendContent(
await this.templateService.renderTemplateFile("add-hosting/netlify/netlify.toml", {
nodeVersion: this.cliService.getNodeVersion(),
projectBranch: await this.gitService.getGitCurrentBranch(realpath, "master"),
projectPath: realpath,
projectName,
})
)
.saveFile();
const netlifyFile = await this.fileFactory.fromFile<TomlFile>(netlifyFilePath);
netlifyFile.appendContent(
await this.templateService.renderTemplateFile("add-hosting/netlify/netlify.toml", {
nodeVersion: this.cliService.getNodeVersion(),
projectBranch: await this.gitService.getGitCurrentBranch(realpath, "master"),
projectPath: realpath,
projectName,
})
);
await netlifyFile.saveFile();

// Configure netlify

Expand Down
15 changes: 7 additions & 8 deletions src/actions/add-ui-framework/adapters/UIBootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ export default class UIBootstrap extends AbstractUIFrameworkAdapter {

// Import style files
this.consoleService.info("Import style files...");
const mainStyleFile = resolve(realpath, "src/index.scss");
const mainStyleFilePath = resolve(realpath, "src/index.scss");

await this.fileFactory
.fromFile(mainStyleFile)
.appendContent(
`// Import Bootstrap and its default variables
const mainStyleFile = await this.fileFactory.fromFile(mainStyleFilePath);
mainStyleFile.appendContent(
`// Import Bootstrap and its default variables
@import '~bootstrap/scss/bootstrap.scss';
`
)
.saveFile();
);
await mainStyleFile.saveFile();

this.consoleService.success(`Style files have been imported in "${mainStyleFile}"`);
this.consoleService.success(`Style files have been imported in "${mainStyleFilePath}"`);
}
}
10 changes: 5 additions & 5 deletions src/actions/add-ui-framework/adapters/UIFrameworkAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export abstract class AbstractUIFrameworkAdapter

// Add UI components to existing App components
this.consoleService.info("Add UI components to existing components...");
const appFile = await this.getAppFilePath(realpath);
await this.fileFactory
.fromFile<TypescriptFile>(appFile)
const appFilePath = await this.getAppFilePath(realpath);
const appFile = await this.fileFactory.fromFile<TypescriptFile>(appFilePath);
appFile
.setImports(
[
{
Expand All @@ -59,8 +59,8 @@ export abstract class AbstractUIFrameworkAdapter
},
]
)
.replaceContent(/ui: undefined,.*$/m, "ui: useUIContextProviderProps(),")
.saveFile();
.replaceContent(/ui: undefined,.*$/m, "ui: useUIContextProviderProps(),");
await appFile.saveFile();

this.consoleService.success("UI components have been added to existing components");
}
Expand Down
25 changes: 14 additions & 11 deletions src/actions/create-app/adapters/CreateAppAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LazyServiceIdentifer, inject, injectable } from "inversify";
import { CliService } from "../../../services/CliService";
import { ConsoleService } from "../../../services/ConsoleService";
import { FileFactory } from "../../../services/file/FileFactory";
import { DirectoryService } from "../../../services/file/DirectoryService";
import { FileService } from "../../../services/file/FileService";
import { TypescriptFile } from "../../../services/file/TypescriptFile";
import { TypescriptImport } from "../../../services/file/TypescriptImport";
Expand Down Expand Up @@ -58,6 +59,7 @@ export abstract class AbstractCreateAppAdapter

constructor(
@inject(FileService) protected readonly fileService: FileService,
@inject(DirectoryService) protected readonly directoryService: DirectoryService,
@inject(ConsoleService) protected readonly consoleService: ConsoleService,
@inject(new LazyServiceIdentifer(() => AddUIFramework))
protected readonly addUIFramework: AddUIFramework,
Expand Down Expand Up @@ -143,7 +145,8 @@ export abstract class AbstractCreateAppAdapter
realpath: string,
shouldPrompt = true
): Promise<boolean | undefined> {
if (this.fileService.dirExistsSync(realpath)) {
const appExists = await this.directoryService.dirExists(realpath);
if (appExists) {
if (shouldPrompt) {
const override = await this.cliService.promptToContinue(
`Directory "${realpath}" exists already`,
Expand All @@ -159,7 +162,8 @@ export abstract class AbstractCreateAppAdapter
}

const parentDir = dirname(realpath);
if (!this.fileService.dirExistsSync(parentDir)) {
const parentDirExists = await this.directoryService.dirExists(parentDir);
if (!parentDirExists) {
this.consoleService.error(
`Unable to create app "${basename(realpath)}", directory "${parentDir}" does not exist.`
);
Expand Down Expand Up @@ -207,15 +211,14 @@ export abstract class AbstractCreateAppAdapter
// Import and add translations as i18n ressources
const entrypointPath = resolve(realpath, this.getEntrypointFilePath());
const importPath = join(".", relative(entrypointPath, i18nPath));
await this.fileFactory
.fromFile<TypescriptFile>(entrypointPath)
.setImports([
{
packageName: importPath,
modules: { [TypescriptImport.defaultImport]: TypescriptImport.defaultImport },
},
])
.saveFile();
const entrypointFile = await this.fileFactory.fromFile<TypescriptFile>(entrypointPath);
entrypointFile.setImports([
{
packageName: importPath,
modules: { [TypescriptImport.defaultImport]: TypescriptImport.defaultImport },
},
]);
await entrypointFile.saveFile();

this.consoleService.success(
`I18n configuration has been created in "${resolve(realpath, i18nPath)}"`
Expand Down

0 comments on commit 7fc348b

Please sign in to comment.