Skip to content

Commit aab715d

Browse files
brianlenzsobolk
andauthored
fix(amplify-cli-core): use build script properly for overrides (#14093)
* fix(amplify-cli-core): use build script properly for overrides Updated the TypeScript compilation of overrides so that it doesn't require `node_modules/.bin/tsc`. Instead, it simply relies on the `build` script to execute `tsc`. This is more flexible and can support alternative setups w/ hoisting (e.g. via Yarn workspaces). This is an override corollary fix to #11854, which is for custom resources. This is an improvement over my previous PR in #13858 in that it works with any package manager by ensuring the `--project` and `tsconfig.json` files are passed through to the `tsc` script. The previous implementation didn't work with `npm` because it doesn't pass through additional args like `yarn` does. The fix was easy: simply separate the build run with `--` so that the remaining args are treated as positional for the `tsc` script. I've confirmed the fix works with both `yarn` and `npm` 💪 #11889 * fix(amplify-cli-core): only include -- for npm Tweaked the implementation slightly so that the `--` arg is only passed for `npm` since it doesn't actually work with `yarn`. #11889 * fix(amplify-cli-core): new packageManner runner Added a new `runner` field to the PackageManager interface. The key difference here is npm's runner is actually `npx`, whereas yarn and pnpm just use the same executable as the runner. Updated both the override and custom-resources to use the runner to run tsc now instead of only looking in `node_modules`. #11889 * fix: update api --------- Co-authored-by: Kamil Sobol <[email protected]>
1 parent 4d368b3 commit aab715d

File tree

4 files changed

+10
-23
lines changed

4 files changed

+10
-23
lines changed

packages/amplify-category-custom/src/utils/build-custom-resources.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,8 @@ const buildResource = async (resource: ResourceMeta): Promise<void> => {
9898
}
9999
}
100100

101-
// get locally installed tsc executable
102-
103-
const localTscExecutablePath = path.join(targetDir, 'node_modules', '.bin', 'tsc');
104-
105-
if (!fs.existsSync(localTscExecutablePath)) {
106-
throw new AmplifyError('MissingOverridesInstallationRequirementsError', {
107-
message: 'TypeScript executable not found.',
108-
resolution: 'Please add it as a dev-dependency in the package.json file for this resource.',
109-
});
110-
}
111-
112101
try {
113-
execa.sync(localTscExecutablePath, {
102+
execa.sync(packageManager.runner, ['tsc'], {
114103
cwd: targetDir,
115104
stdio: 'pipe',
116105
encoding: 'utf-8',

packages/amplify-cli-core/API.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,8 @@ export interface PackageManager {
14991499
// (undocumented)
15001500
readonly packageManager: PackageManagerType;
15011501
// (undocumented)
1502+
readonly runner: string;
1503+
// (undocumented)
15021504
version?: SemVer;
15031505
}
15041506

packages/amplify-cli-core/src/overrides-manager/override-skeleton-generator.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,7 @@ export const buildOverrideDir = async (cwd: string, destDirPath: string): Promis
9393
const tsConfigSampleFilePath = path.join(__dirname, '..', '..', 'resources', 'overrides-resource', 'tsconfig.resource.json');
9494
fs.writeFileSync(tsConfigDestFilePath, fs.readFileSync(tsConfigSampleFilePath));
9595

96-
// get locally installed tsc executable
97-
98-
const localTscExecutablePath = path.join(cwd, 'node_modules', '.bin', 'tsc');
99-
100-
if (!fs.existsSync(localTscExecutablePath)) {
101-
throw new AmplifyError('MissingOverridesInstallationRequirementsError', {
102-
message: 'TypeScript executable not found.',
103-
resolution: 'Please add it as a dev-dependency in the package.json file for this resource.',
104-
});
105-
}
106-
execa.sync(localTscExecutablePath, [`--project`, `${tsConfigDestFilePath}`], {
96+
execa.sync(packageManager.runner, ['tsc', '--project', tsConfigDestFilePath], {
10797
cwd: tsConfigDir,
10898
stdio: 'pipe',
10999
encoding: 'utf-8',

packages/amplify-cli-core/src/utils/packageManager.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface PackageManager {
1919
readonly packageManager: PackageManagerType;
2020
readonly lockFile: string;
2121
readonly executable: string;
22+
readonly runner: string;
2223
readonly displayValue: string;
2324
version?: SemVer;
2425
getRunScriptArgs: (scriptName: string) => string[];
@@ -29,6 +30,7 @@ class NpmPackageManager implements PackageManager {
2930
readonly packageManager = 'npm';
3031
readonly displayValue = 'NPM';
3132
readonly executable = 'npm';
33+
readonly runner = 'npx';
3234
readonly lockFile = 'package-lock.json';
3335

3436
getRunScriptArgs = (scriptName: string) => ['run-script', scriptName];
@@ -39,6 +41,7 @@ class YarnPackageManager implements PackageManager {
3941
readonly packageManager: PackageManagerType = 'yarn';
4042
readonly displayValue = 'Yarn';
4143
readonly executable = 'yarn';
44+
readonly runner = this.executable;
4245
readonly lockFile = 'yarn.lock';
4346
version?: SemVer;
4447

@@ -66,6 +69,7 @@ class PnpmPackageManager implements PackageManager {
6669
readonly packageManager: PackageManagerType = 'pnpm';
6770
readonly displayValue = 'PNPM';
6871
readonly executable = 'pnpm';
72+
readonly runner = this.executable;
6973
readonly lockFile = 'pnpm-lock.yaml';
7074

7175
getRunScriptArgs = (scriptName: string) => [scriptName];
@@ -77,11 +81,13 @@ class CustomPackageManager implements PackageManager {
7781
readonly displayValue = 'Custom Build Command or Script Path';
7882
lockFile;
7983
executable;
84+
runner;
8085
version?: SemVer;
8186

8287
constructor() {
8388
this.lockFile = '';
8489
this.executable = '';
90+
this.runner = '';
8591
}
8692
getRunScriptArgs = () => {
8793
throw new AmplifyError('PackagingLambdaFunctionError', {

0 commit comments

Comments
 (0)