Skip to content

Commit 76fe61b

Browse files
committed
daff sync command
1 parent ced1106 commit 76fe61b

3 files changed

Lines changed: 156 additions & 0 deletions

File tree

tools/schematics/bin/daff.spec.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { syncProjects } from './daff';
2+
3+
const buildAngularJson = (projectName: string) => ({
4+
version: 1,
5+
projects: {
6+
[projectName]: {
7+
architect: {
8+
build: {
9+
configurations: {
10+
production: {},
11+
},
12+
},
13+
},
14+
},
15+
},
16+
});
17+
18+
describe('syncProjects', () => {
19+
const projectName = 'app';
20+
21+
it('writes the magento condition derived from daff.json', () => {
22+
const angular = buildAngularJson(projectName);
23+
const daff = { projects: { [projectName]: { drivers: { magento: '2.4.5' }}}};
24+
25+
syncProjects(daff, angular);
26+
27+
expect(
28+
angular.projects[projectName].architect.build.configurations.production,
29+
).toEqual(<any>{ conditions: ['magento-2.4.5']});
30+
});
31+
32+
it('overwrites an existing conditions array', () => {
33+
const angular = buildAngularJson(projectName);
34+
(<any>angular.projects[projectName].architect.build.configurations.production).conditions = [
35+
'magento-2.4.1',
36+
'some-other-condition',
37+
];
38+
const daff = { projects: { [projectName]: { drivers: { magento: '2.4.5' }}}};
39+
40+
syncProjects(daff, angular);
41+
42+
const conditions = (<any>angular.projects[projectName].architect.build.configurations.production).conditions;
43+
expect(conditions).toEqual(['magento-2.4.5']);
44+
});
45+
46+
it('throws when a project is missing a driver version', () => {
47+
const angular = buildAngularJson(projectName);
48+
const daff = { projects: { [projectName]: { drivers: {}}}};
49+
50+
expect(() => syncProjects(daff, angular)).toThrowError(
51+
/missing a driver version/,
52+
);
53+
});
54+
55+
it('throws when the project is absent from angular.json', () => {
56+
const angular = buildAngularJson('other');
57+
const daff = { projects: { [projectName]: { drivers: { magento: '2.4.5' }}}};
58+
59+
expect(() => syncProjects(daff, angular)).toThrowError(
60+
/not found in angular.json/,
61+
);
62+
});
63+
64+
it('creates intermediate architect/build/configurations objects when missing', () => {
65+
const angular = { projects: { [projectName]: {}}};
66+
const daff = { projects: { [projectName]: { drivers: { magento: '2.4.5' }}}};
67+
68+
syncProjects(daff, angular);
69+
70+
expect(
71+
(<any>angular).projects[projectName].architect.build.configurations.production.conditions,
72+
).toEqual(['magento-2.4.5']);
73+
});
74+
75+
it('is a no-op when daff.json has no projects', () => {
76+
const angular = buildAngularJson(projectName);
77+
const daff = {};
78+
79+
expect(() => syncProjects(daff, angular)).not.toThrow();
80+
expect(
81+
(<any>angular.projects[projectName].architect.build.configurations.production).conditions,
82+
).toBeUndefined();
83+
});
84+
});

tools/schematics/bin/daff.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env node
2+
import * as fs from 'fs';
3+
import * as path from 'path';
4+
5+
export interface DaffJson {
6+
projects?: Record<string, { drivers?: { magento?: string } }>;
7+
}
8+
9+
export const syncProjects = (daff: DaffJson, angular: any): any => {
10+
for (const [projectName, cfg] of Object.entries(daff.projects ?? {})) {
11+
const version = cfg.drivers?.magento;
12+
if (!version) {
13+
throw new Error(
14+
`Project '${projectName}' in daff.json is missing a driver version; cannot sync.`,
15+
);
16+
}
17+
18+
const project = angular.projects?.[projectName];
19+
if (!project) {
20+
throw new Error(`Project '${projectName}' not found in angular.json.`);
21+
}
22+
23+
project.architect ??= {};
24+
project.architect.build ??= {};
25+
project.architect.build.configurations ??= {};
26+
project.architect.build.configurations.production ??= {};
27+
project.architect.build.configurations.production.conditions = [`magento-${version}`];
28+
}
29+
30+
return angular;
31+
};
32+
33+
const main = (): void => {
34+
const [, , cmd] = process.argv;
35+
if (cmd !== 'sync') {
36+
console.error('Usage: daff sync');
37+
process.exit(1);
38+
}
39+
40+
const cwd = process.cwd();
41+
const daffPath = path.join(cwd, 'daff.json');
42+
const ngPath = path.join(cwd, 'angular.json');
43+
44+
if (!fs.existsSync(daffPath)) {
45+
console.error(`daff.json not found at ${daffPath}`);
46+
process.exit(1);
47+
}
48+
if (!fs.existsSync(ngPath)) {
49+
console.error(`angular.json not found at ${ngPath}`);
50+
process.exit(1);
51+
}
52+
53+
const daff = <DaffJson>JSON.parse(fs.readFileSync(daffPath, 'utf-8'));
54+
const angular = JSON.parse(fs.readFileSync(ngPath, 'utf-8'));
55+
56+
try {
57+
syncProjects(daff, angular);
58+
} catch (e) {
59+
console.error((<Error>e).message);
60+
process.exit(1);
61+
}
62+
63+
fs.writeFileSync(ngPath, JSON.stringify(angular, null, 2) + '\n');
64+
console.warn('angular.json updated.');
65+
};
66+
67+
if (require.main === module) {
68+
main();
69+
}

tools/schematics/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"author": "Daffodil Team",
1414
"license": "MIT",
1515
"schematics": "./collection.json",
16+
"bin": {
17+
"daff": "./bin/daff.js"
18+
},
1619
"ng-add": {
1720
"save": "dependencies"
1821
},

0 commit comments

Comments
 (0)