Skip to content

Commit

Permalink
chore: refactor getPackageManagerController yarn-modern
Browse files Browse the repository at this point in the history
  • Loading branch information
0618 committed Dec 18, 2023
1 parent 24e17eb commit 28da8a9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,13 @@ export class PackageManagerControllerFactory {
/**
* Copies the template directory to an amplify folder within the projectRoot
*/
generateInitialProjectFiles = async (packageManagerProps: {
name: string;
binaryRunner: string;
}): Promise<void> => {
generateInitialProjectFiles = async (
packageManagerProps: {
name: string;
binaryRunner: string;
},
operate?: (param: string) => Promise<void>
): Promise<void> => {
const targetDir = path.resolve(this.projectRoot, 'amplify');
await this.fsp.mkdir(targetDir, { recursive: true });
await this.fsp.cp(
Expand All @@ -206,15 +209,10 @@ export class PackageManagerControllerFactory {
JSON.stringify(packageJsonContent, null, 2)
);

if (packageManagerProps.name === 'yarn-modern') {
try {
await this.fsp.writeFile(path.resolve(targetDir, 'yarn.lock'), '');
console.log(`${targetDir}/yarn.lock created successfully.`);
} catch (error) {
console.error(`Error creating ${targetDir}/${targetDir}`, error);
}
}

await this.initializeTsConfig(targetDir, packageManagerProps);

if (operate) {
await operate(targetDir);
}
};
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { execa as _execa } from 'execa';
import fsp from 'fs/promises';
import * as path from 'path';
import { executeWithDebugLogger } from '../execute_with_logger.js';
import {
DependencyType,
Expand All @@ -12,6 +14,7 @@ import {
export class YarnModernPackageManagerController
implements PackageManagerController
{
private readonly fsp = fsp;
private readonly execa = _execa;
private readonly packageManagerProps = {
name: 'yarn-modern',
Expand All @@ -29,6 +32,18 @@ export class YarnModernPackageManagerController
private readonly packageManagerControllerFactory: PackageManagerControllerFactory
) {}

/**
* createLockFile
*/
private async createLockFile(targetDir: string) {
try {
await this.fsp.writeFile(path.resolve(targetDir, 'yarn.lock'), '');
console.log(`${targetDir}/yarn.lock created successfully.`);
} catch (error) {
console.error(`Error creating ${targetDir}/${targetDir}`, error);
}
}

/**
* Installs the given package names as devDependencies
*/
Expand Down Expand Up @@ -59,7 +74,8 @@ export class YarnModernPackageManagerController

generateInitialProjectFiles = async () => {
await this.packageManagerControllerFactory.generateInitialProjectFiles(
this.packageManagerProps
this.packageManagerProps,
this.createLockFile
);
};

Expand Down

0 comments on commit 28da8a9

Please sign in to comment.