Skip to content
This repository has been archived by the owner on Oct 14, 2022. It is now read-only.

Commit

Permalink
feat(@architect/udk): enhance generate package json
Browse files Browse the repository at this point in the history
  • Loading branch information
enten committed Apr 12, 2021
1 parent e8ed9f9 commit b24c486
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 210 deletions.
2 changes: 1 addition & 1 deletion angular/README.md
Expand Up @@ -16,7 +16,7 @@ Build an universal angular application.
| `deleteOutputPath` | boolean | false | Delete the output path before building. |
| `verbose` | boolean | false | Adds more details to output logging. |
| `outputPath` | string | | The full path for the output directory (relative to the current workspace) expected of browser and server targets. Use it to warn when browser or server target output path is outside this output path. Use it to allow nx (@nrwl/workspace) to cache udk build. Don't use it in case of browser and server targets hasn't the same base output path. |
| `generatePackageJson` | boolean | false | Generates a package.json file inside 'outputPath' with a main field to server output main.js. |
| `generatePackageJson` | boolean | false | Generates a package.json file inside 'outputPath' with a main field to server output main.js. If a package.json exists in the project's directory, it will be reused. |

### udk:udk-runner

Expand Down
2 changes: 1 addition & 1 deletion angular/lib/build/index.ts
Expand Up @@ -124,7 +124,7 @@ export async function ngUniversalBuild(
emittedFiles: serverEmittedFiles,
} as BuildResult);

generatePackageJson(context, options, serverOptions);
await generatePackageJson(context, options, serverOptions);

context.reportStatus(`Done.`);

Expand Down
38 changes: 28 additions & 10 deletions angular/lib/build/ng-devkit.ts
Expand Up @@ -174,11 +174,11 @@ export async function validateTargetOptions<T>(

// #region universal utils

export function generatePackageJson(
export async function generatePackageJson(
context: BuilderContext,
universalOptions: UniversalBuildOptions,
serverOptions: ServerBuilderOptions,
): void {
): Promise<void> {
if (!context.target || !universalOptions.generatePackageJson || !universalOptions.outputPath) {
return;
}
Expand All @@ -188,10 +188,11 @@ export function generatePackageJson(
resolve(normalize(context.workspaceRoot), normalize(serverOptions.outputPath)),
);
const mainPathRelative = join(serverOutputPathRelative, 'main.js');
const workspacePackageJsonPath = path.resolve(context.workspaceRoot, 'package.json');
let workspacePackageVersion = '0.0.0';

try {
const workspacePackageJson = require(path.resolve(context.workspaceRoot, 'package.json'));
const workspacePackageJson = require(workspacePackageJsonPath);

if (workspacePackageJson?.version) {
workspacePackageVersion = workspacePackageJson.version;
Expand All @@ -200,16 +201,33 @@ export function generatePackageJson(
context.logger.error('Error while reading workspace package.json:', err);
}

const packageJson = `{
"private": true,
"name": "${context.target.project}",
"version": "${workspacePackageVersion}",
"main": "./${mainPathRelative}"
}`;
const projectMetadata = await context.getProjectMetadata(context.target);
const projectPackageJsonPath = path.resolve(context.workspaceRoot, projectMetadata.root as string, 'package.json');
let projectPackageJson: json.JsonObject = {};

if (
projectMetadata.root
&& projectPackageJsonPath !== workspacePackageJsonPath
&& fs.existsSync(projectPackageJsonPath)
) {
projectPackageJson = require(projectPackageJsonPath);
}

const packageJson = {
private: typeof projectPackageJson.private === 'boolean' ? projectPackageJson.private : true,
name: context.target.project,
version: '0.0.0',
main: mainPathRelative,
...projectPackageJson,
};

packageJson.version = projectPackageJson.version && projectPackageJson.version !== '0.0.0'
? projectPackageJson.version as string
: workspacePackageVersion;

fs.writeFileSync(
path.resolve(context.workspaceRoot, universalOptions.outputPath, 'package.json'),
packageJson,
JSON.stringify(packageJson, null, 2),
'utf8',
);

Expand Down
2 changes: 1 addition & 1 deletion angular/lib/build/schema.json
Expand Up @@ -57,7 +57,7 @@
},
"generatePackageJson": {
"type": "boolean",
"description": "Generates a package.json file inside 'outputPath' with a main field to server output main.js.",
"description": "Generates a package.json file inside 'outputPath' with a main field to server output main.js. If a package.json exists in the project's directory, it will be reused.",
"default": false
}
},
Expand Down
4 changes: 2 additions & 2 deletions angular/package.json
Expand Up @@ -2,7 +2,7 @@
"private": true,
"name": "@architect/udk",
"peerDependencies": {
"@angular-devkit/build-angular": "0.1102.7",
"@angular/cli": "11.2.7"
"@angular-devkit/build-angular": "0.1102.8",
"@angular/cli": "11.2.8"
}
}

0 comments on commit b24c486

Please sign in to comment.