Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLey committed Nov 28, 2023
1 parent 6537eb7 commit bf63c5a
Show file tree
Hide file tree
Showing 21 changed files with 1,041 additions and 763 deletions.
18 changes: 18 additions & 0 deletions apps/nx-lifecycle/.c8rc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"reporter": ["html", "text-summary"],
"exclude": ["coverage/**"],
"exclude-after-remap": true,
"extension": [".js", ".cjs", ".mjs", ".ts", ".cts", ".mts", ".tsx", ".jsx"],

"lines": 100,
"statements": 100,
"functions": 100,
"branches": 100,

"watermarks": {
"lines": [100, 95],
"functions": [100, 95],
"branches": [100, 95],
"statements": [100, 95]
}
}
16 changes: 0 additions & 16 deletions apps/nx-lifecycle/.mocharc.cjs

This file was deleted.

4 changes: 2 additions & 2 deletions apps/nx-lifecycle/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ tsconfig.json

dist/tsconfig.tsbuildinfo
dist/**/*.map
dist/**/*.spec.*js

src/**
!src/executors/lifecycle/schema.json

dist/file-content.js
dist/file-content.d.ts

dist/executors/lifecycle/schema.js
dist/executors/lifecycle/schema.d.ts

!src/executors/lifecycle/schema.json
1 change: 1 addition & 0 deletions apps/nx-lifecycle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@types/mocha": "^10.0.3",
"@types/node": "^20.8.10",
"chai": "^4.3.10",
"c8": "8.0.1",
"load-populate-files": "workspace:^",
"mocha": "^10.2.0",
"nx-populate-files": "workspace:^",
Expand Down
20 changes: 18 additions & 2 deletions apps/nx-lifecycle/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,29 @@
"mocha": {
"executor": "nx:run-commands",
"options": {
"command": "mocha ./dist/tests/unit",
"command": "c8 --reporter=none mocha --recursive ./dist/**/*.test.*js",
"description": "Run unit test suite",
"color": true,
"cwd": "{projectRoot}"
},
"dependsOn": [
"build"
]
],
"inputs": ["coverage-configs", "ts-source", "prod-data", "test-data", "^ts-source", "^prod-data"],
"outputs": ["{projectRoot}/coverage/tmp"]
},
"report": {
"executor": "nx:run-commands",
"options": {
"command": "c8 report --all --check-coverage",
"description": "Create html report of coverage and report if failing threshold",
"color": true,
"cwd": "{projectRoot}"
},
"dependsOn": [
"mocha"
],
"inputs": ["coverage-configs", "{projectRoot}/coverage"]
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions apps/nx-lifecycle/src/executors/lifecycle/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readFile, writeFile } from 'node:fs/promises';
import type { ExecutorContext } from '@nx/devkit';
import { isCI } from 'ci-info';
import type { LifecycleOptions } from './schema.js';
import { lifecycle } from './lifecycle.js';

Expand All @@ -10,6 +11,7 @@ export default async (

try {
return await lifecycle(options, context, {
isCI,
readFile,
writeFile,
});
Expand Down
52 changes: 37 additions & 15 deletions apps/nx-lifecycle/src/executors/lifecycle/lifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import type fs from 'node:fs/promises';
import type { ExecutorContext } from '@nx/devkit';
import { isNxJson, isProjectJson, type NxJson, type ProjectJson } from '#schemas';
import { normalizeOptions, type NormalizedOptions } from './normalizer.js';
import { processNxAndProjectJsons } from './processor.js';
import type { LifecycleDI, SimpleExecutorContext } from './types.js';

import type { LifecycleOptions } from './schema.js';

interface LoadedJsonConfig<T> {
name: string;
path: string;
rawData: string;
data: T;
};
const loadJsonConfigs = async (
Expand All @@ -18,7 +17,7 @@ const loadJsonConfigs = async (
}: NormalizedOptions,
{
readFile
}: Pick<typeof fs, 'readFile'>
}: Pick<LifecycleDI, 'readFile'>
): Promise<{
nxJson: LoadedJsonConfig<NxJson>;
projectJsons: LoadedJsonConfig<ProjectJson>[];
Expand All @@ -29,7 +28,8 @@ const loadJsonConfigs = async (
...rawProjectJsons
] = await Promise.all([
readFile(nxJsonPath, 'utf8'),
...packageJsonPaths.map(async path => ({
...packageJsonPaths.map(async ({ name, path }) => ({
name,
path,
rawData: await readFile(path, 'utf8'),
})),
Expand All @@ -42,11 +42,11 @@ const loadJsonConfigs = async (

return {
nxJson: {
name: 'nx.json',
path: nxJsonPath,
rawData: rawNxJson,
data: parsedNxJson,
},
projectJsons: rawProjectJsons.map(({ path, rawData }) => {
projectJsons: rawProjectJsons.map(({ name, path, rawData }) => {

const data = JSON.parse(rawData);

Expand All @@ -55,24 +55,35 @@ const loadJsonConfigs = async (
}

return {
name,
path,
rawData,
data,
};
}),
};
};

interface ProcessedJsonConfig<T> extends LoadedJsonConfig<T> {
processed: T;
};
const saveJsonConfigs = async ({ jsons, options }: {
jsons: ProcessedJsonConfig<unknown>[];
options: NormalizedOptions;
}, { writeFile }: Pick<LifecycleDI, 'writeFile'>): Promise<void> => {

};

export const lifecycle = async (
options: LifecycleOptions,
context: ExecutorContext,
context: SimpleExecutorContext,
{
isCI,
readFile,
// writeFile,
}: Pick<typeof fs, 'readFile' | 'writeFile'>
writeFile,
}: LifecycleDI
): Promise<{ success: boolean }> => {

const normalized = normalizeOptions(options, context);
const normalized = normalizeOptions(options, context, { isCI });

const { nxJson, projectJsons } = await loadJsonConfigs(normalized, { readFile });

Expand All @@ -85,8 +96,19 @@ export const lifecycle = async (
options: normalized,
});

console.log(JSON.stringify(processedNxJson, null, 2));
console.log(JSON.stringify(processedProjectJsons, null, 2));
await saveJsonConfigs({
jsons: [
{
...nxJson,
processed: processedNxJson,
},
...projectJsons.map((projectJson, i) => ({
...projectJson,
processed: processedProjectJsons[i]!,
})),
],
options: normalized,
}, { writeFile });

return { success: false };
return { success: true };
};
86 changes: 86 additions & 0 deletions apps/nx-lifecycle/src/executors/lifecycle/normalizer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { expect } from 'chai';
import { suite, test } from 'mocha';
import * as Normalizer from './normalizer.js';

suite('Normalizer', () => {

suite('normalizeOptions', () => {

test('Use provided options', () => {

expect(Normalizer.normalizeOptions(
{
check: false,
dryRun: true,
stages: {
myStage: {},
},
targets: {
myTarget: 'myStage',
},
},
{
root: '/path/to/workspace',
projectsConfigurations: {
version: 123,
projects: {
foo: {
root: 'path/to/foo',
},
bar: {
root: 'path/to/bar',
},
},
},
},
{ isCI: true }
)).to.deep.equal({
check: false,
dryRun: true,
nxJsonPath: '/path/to/workspace/nx.json',
packageJsonPaths: [
'/path/to/workspace/path/to/foo/project.json',
'/path/to/workspace/path/to/bar/project.json',
],
stages: {
myStage: {},
},
targets: {
myTarget: 'myStage',
},
});
});

test('Use defaults', () => {

expect(Normalizer.normalizeOptions(
{},
{
root: '/path/to/workspace',
projectsConfigurations: {
version: 123,
projects: {
foo: {
root: 'path/to/foo',
},
bar: {
root: 'path/to/bar',
},
},
},
},
{ isCI: true }
)).to.deep.equal({
check: true,
dryRun: false,
nxJsonPath: '/path/to/workspace/nx.json',
packageJsonPaths: [
'/path/to/workspace/path/to/foo/project.json',
'/path/to/workspace/path/to/bar/project.json',
],
stages: {},
targets: {},
});
});
});
});
15 changes: 10 additions & 5 deletions apps/nx-lifecycle/src/executors/lifecycle/normalizer.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import { join } from 'node:path';
import type { ExecutorContext } from '@nx/devkit';
import { isCI } from 'ci-info';
import type { LifecycleOptions } from './schema.js';
import type { LifecycleDI, SimpleExecutorContext } from './types.js';

export interface NormalizedOptions {
check: boolean;
dryRun: boolean;
nxJsonPath: string;
packageJsonPaths: string[];
packageJsonPaths: { name: string; path: string }[];
stages: NonNullable<LifecycleOptions['stages']>;
targets: NonNullable<LifecycleOptions['targets']>;
}

export const normalizeOptions = (
options: LifecycleOptions,
context: ExecutorContext
context: SimpleExecutorContext,
{ isCI }: Pick<LifecycleDI, 'isCI'>
): NormalizedOptions => ({
check: options.check ?? isCI,
dryRun: options.dryRun ?? false,
nxJsonPath: join(context.root, 'nx.json'),
packageJsonPaths: Object.values(context.projectsConfigurations!.projects).map(
(projectConfig) => join(context.root, projectConfig.root, 'project.json')
(projectConfig) => ({
name: projectConfig.name!,
path: join(context.root, projectConfig.root, 'project.json'),
})
),
stages: options.stages ?? {},
targets: options.targets ?? {},
});

0 comments on commit bf63c5a

Please sign in to comment.