Skip to content
This repository was archived by the owner on Nov 21, 2024. It is now read-only.

Commit b1271be

Browse files
committed
feat(base): Add updateDependencies method and run it after dist npm link
1 parent c6fc53a commit b1271be

4 files changed

Lines changed: 54 additions & 10 deletions

File tree

src/lib/apps.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class Apps extends Base {
3434
}
3535
return results.reduce((all: boolean, current: boolean) => { return all && current; }, true);
3636
}
37-
async link(customOptions?: { package?: any, packages?: any[], folder?: string, srcFolder?: string }) {
37+
async link(customOptions?: { package?: any, packages?: any[], folder?: string, srcFolder?: string, rootPackagePath?: string, distPackagePath?: string }) {
3838
const results = [];
3939
for (let i = 0; i < this.folders.length; i++) {
4040
const app = new App(this.folders[i], this.rootFolder);
@@ -47,7 +47,7 @@ export class Apps extends Base {
4747
}
4848
return results.reduce((all: boolean, current: boolean) => { return all && current; }, true);
4949
}
50-
async linkNpm(customOptions?: { package?: any, packages?: any[], folder?: string, srcFolder?: string }) {
50+
async linkNpm(customOptions?: { package?: any, packages?: any[], folder?: string, srcFolder?: string, rootPackagePath?: string, srcPackagePath?: string }) {
5151
const results = [];
5252
for (let i = 0; i < this.folders.length; i++) {
5353
const app = new App(this.folders[i], this.rootFolder);

src/lib/base.ts

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class Base {
6363
commandArgs,
6464
{ cwd: cwdDir ? cwdDir : process.cwd() }
6565
);
66-
const stderrWords=child.stderr ? _.words(child.stderr.toString()).filter((word:string)=>word):[];
66+
const stderrWords = child.stderr ? _.words(child.stderr.toString()).filter((word: string) => word) : [];
6767
if (child.status === 1 || child.status === '1' || stderrWords.length > 0) {
6868
this.log('commandRunner').error('status', child.status);
6969
this.log('commandRunner').error('stderr', child.stderr.toString());
@@ -86,6 +86,7 @@ export class Base {
8686
}
8787
this.log('clear').debug(folder);
8888
const commandString = './node_modules/.bin/del-cli ' +
89+
path.resolve(folder + '/src/package-lock.json') + ' ' +
8990
path.resolve(folder + '/src/node_modules') + ' ' +
9091
path.resolve(folder + '/dist') + ' ' +
9192
path.resolve(folder + '/.tmp') + ' --force';
@@ -106,7 +107,7 @@ export class Base {
106107
folder = customOptions.srcFolder;
107108
}
108109
this.log('build').debug(folder);
109-
110+
110111
const commandString = 'ng-packagr -p ' + folder + '/ng-package.json';
111112
if (!fsExtra.existsSync(folder)) {
112113
this.log('build').debug(commandString);
@@ -118,12 +119,13 @@ export class Base {
118119
this.log('build').debug('end');
119120
return result;
120121
}
121-
async link(customOptions?: { folder?: string, srcFolder?: string }) {
122+
async link(customOptions?: { folder?: string, srcFolder?: string, rootPackagePath?: string, distPackagePath?: string }) {
122123
this.log('link').debug('start');
123124
const items = [
124125
await this.linkNpmClear(customOptions),
125126
await this.linkDist(customOptions),
126-
await this.linkNpmClear(customOptions)
127+
await this.linkNpmClear(customOptions),
128+
await this.updateDependencies(customOptions),
127129
];
128130
this.log('link').debug('end');
129131
return items.reduce((all: boolean, current: boolean) => { return all && current; }, true);
@@ -182,6 +184,8 @@ export class Base {
182184
}
183185
this.log('linkNpmClear').debug(folder);
184186
const commandString = './node_modules/.bin/del-cli ' +
187+
path.resolve(folder + '/src/package-lock.json') + ' ' +
188+
path.resolve(folder + '/dist/package-lock.json') + ' ' +
185189
path.resolve(folder + '/src/node_modules') + ' ' +
186190
path.resolve(folder + '/dist/node_modules') + ' --force';
187191
if (!fsExtra.existsSync(folder)) {
@@ -222,6 +226,45 @@ export class Base {
222226
this.log('changeVersion').debug('end');
223227
return await true;
224228
}
229+
async updateDependencies(customOptions?: { rootPackagePath?: string, distPackagePath?: string }) {
230+
this.log('updateDependencies').debug('start');
231+
let rootPackagePath = path.resolve(this.rootFolder + '/package.json');
232+
let distPackagePath = path.resolve(this.folder + '/dist/package.json');
233+
if (customOptions && customOptions.rootPackagePath) {
234+
rootPackagePath = customOptions.rootPackagePath;
235+
}
236+
if (customOptions && customOptions.distPackagePath) {
237+
distPackagePath = customOptions.distPackagePath;
238+
}
239+
240+
let rootPackage: any = null;
241+
let distPackage: any = null;
242+
243+
if (fsExtra.existsSync(rootPackagePath)) {
244+
rootPackage = fsExtra.readJSONSync(rootPackagePath);
245+
}
246+
if (fsExtra.existsSync(distPackagePath)) {
247+
distPackage = fsExtra.readJSONSync(distPackagePath);
248+
}
249+
if (rootPackage && distPackage) {
250+
for (let key in rootPackage) {
251+
if (key.toLowerCase().indexOf('dependencies') !== -1) {
252+
let dependencies = rootPackage[key];
253+
if (distPackage[key]) {
254+
for (let devKey in dependencies) {
255+
if (distPackage[key][devKey] === '*') {
256+
distPackage[key][devKey] = dependencies[devKey];
257+
}
258+
}
259+
}
260+
}
261+
}
262+
fsExtra.writeJSONSync(distPackagePath, distPackage, { spaces: 4 });
263+
this.log('updateDependencies').debug('writeJSONSync');
264+
}
265+
this.log('updateDependencies').debug('end');
266+
return await true;
267+
}
225268
async extractTranslate(customOptions?: { srcFolder?: string }) {
226269
this.log('extractTranslate').debug('start');
227270
let folder = path.resolve(this.folder + '/src');

src/lib/libs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class Libs extends Base {
3232
}
3333
return results.reduce((all: boolean, current: boolean) => { return all && current; }, true);
3434
}
35-
async link(customOptions?: { package?: any, packages?: any[], folder?: string, srcFolder?: string }) {
35+
async link(customOptions?: { package?: any, packages?: any[], folder?: string, srcFolder?: string, rootPackagePath?: string, distPackagePath?: string }) {
3636
const results = [];
3737
for (let i = 0; i < this.folders.length; i++) {
3838
const lib = new Lib(this.folders[i], this.rootFolder);
@@ -44,7 +44,7 @@ export class Libs extends Base {
4444
}
4545
return results.reduce((all: boolean, current: boolean) => { return all && current; }, true);
4646
}
47-
async linkNpm(customOptions?: { package?: any, packages?: any[], folder?: string, srcFolder?: string }) {
47+
async linkNpm(customOptions?: { package?: any, packages?: any[], folder?: string, srcFolder?: string, rootPackagePath?: string, srcPackagePath?: string }) {
4848
const results = [];
4949
for (let i = 0; i < this.folders.length; i++) {
5050
const lib = new Lib(this.folders[i], this.rootFolder);

test/lib._spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ describe('Lib', () => {
8484
});
8585
describe('#link()', () => {
8686
const dir = path.resolve(`${__dirname}/fixture/libs/lib1`);
87+
const dirRoot = path.resolve(__dirname, '..');
8788
const dirDist = path.resolve(`${dir}/../../../../node_modules/@test/lib1`);
8889
const dirDistIndex = path.resolve(`${dirDist}/index.d.ts`);
8990
beforeEach(() => {
@@ -98,7 +99,7 @@ describe('Lib', () => {
9899
assert.equal(fsExtra.existsSync(dirDistIndex), false);
99100
});
100101
it('npm link ./test/fixture/libs/lib1/dist', (done) => {
101-
const lib = new Lib(dir);
102+
const lib = new Lib(dir, dirRoot);
102103
lib.debug = debug;
103104
lib.link().then((data: boolean) => {
104105
assert.equal(fsExtra.existsSync(dirDist), true);
@@ -111,7 +112,7 @@ describe('Lib', () => {
111112
});
112113
describe('#linkNpm()', () => {
113114
const dir = path.resolve(`${__dirname}/fixture/libs/lib1`);
114-
const dirRoot = path.resolve(__dirname);
115+
const dirRoot = path.resolve(__dirname, '..');
115116
const dirDist = path.resolve(`${dir}/../../../../node_modules/@test/lib1`);
116117
const dirDistIndex = path.resolve(`${dirDist}/index.ts`);
117118
beforeEach(() => {

0 commit comments

Comments
 (0)