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

Commit 05b4c01

Browse files
committed
feat(app): Add changeMetaInIndex for prepare app, to write version of package in index.html
1 parent 5d76859 commit 05b4c01

5 files changed

Lines changed: 132 additions & 15 deletions

File tree

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@
6161
"@angular/compiler": "^5.0.4",
6262
"@angular/core": "^5.0.4",
6363
"@biesbjerg/ngx-translate-extract": "^2.3.4",
64+
"@types/cheerio": "^0.22.6",
6465
"@types/commander": "^2.11.0",
6566
"@types/del": "^3.0.0",
6667
"@types/dotenv": "^4.0.2",
6768
"@types/fs-extra": "^4.0.5",
6869
"@types/lodash": "^4.14.86",
6970
"@types/loglevel": "^1.5.3",
71+
"cheerio": "^1.0.0-rc.2",
7072
"commander": "^2.12.2",
7173
"conventional-changelog-cli": "^1.3.5",
7274
"conventional-commits-detector": "^0.1.1",

src/lib/app.ts

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { Base } from './base';
2-
import * as path from 'path';
1+
import * as cheerio from 'cheerio';
32
import * as fsExtra from 'fs-extra';
4-
import * as _ from 'lodash';
3+
import * as path from 'path';
4+
5+
import { Base } from './base';
56

67
export class App extends Base {
78
name = 'app';
@@ -36,6 +37,62 @@ export class App extends Base {
3637
this.log('linkNpmClear').debug('dummy');
3738
return await true;
3839
}
40+
async changeMetaInIndex(customOptions?: {
41+
rootPackagePath?: string,
42+
srcPackagePath?: string,
43+
srcIndexHtmlPath?: string,
44+
srcPackage?: {
45+
version?: string
46+
}
47+
}) {
48+
this.log('changeMetaInIndex').debug('start');
49+
let rootPackagePath = path.resolve(this.rootFolder + '/package.json');
50+
let srcPackagePath = path.resolve(this.folder + '/src/app/package.json');
51+
let srcIndexHtmlPath = path.resolve(this.folder + '/src/index.html');
52+
let srcIndexHtml = '';
53+
let srcPackage: any = {};
54+
if (!customOptions) {
55+
customOptions = {};
56+
}
57+
if (customOptions.rootPackagePath) {
58+
rootPackagePath = customOptions.rootPackagePath;
59+
}
60+
if (customOptions.srcPackagePath) {
61+
srcPackagePath = customOptions.srcPackagePath;
62+
}
63+
if (customOptions.srcIndexHtmlPath) {
64+
srcIndexHtmlPath = customOptions.srcIndexHtmlPath;
65+
}
66+
if (customOptions.srcPackage) {
67+
srcPackage = customOptions.srcPackage;
68+
} else {
69+
if (fsExtra.existsSync(srcPackagePath)) {
70+
srcPackage = fsExtra.readJSONSync(srcPackagePath);
71+
} else {
72+
if (fsExtra.existsSync(rootPackagePath)) {
73+
srcPackage = fsExtra.readJSONSync(rootPackagePath);
74+
}
75+
}
76+
}
77+
this.log('changeMetaInIndex').debug('srcPackagePath', srcPackagePath);
78+
this.log('changeMetaInIndex').debug('srcIndexHtmlPath', srcIndexHtmlPath);
79+
this.log('changeMetaInIndex').debug('srcPackage', srcPackage);
80+
if (fsExtra.existsSync(srcIndexHtmlPath)) {
81+
srcIndexHtml = fsExtra.readFileSync(srcIndexHtmlPath).toString();
82+
this.log('changeMetaInIndex').debug('readFileSync', srcIndexHtml);
83+
const $ = cheerio.load(srcIndexHtml);
84+
$('meta[name=version]').attr('content', srcPackage.version);
85+
srcIndexHtml = $.html();
86+
fsExtra.writeFileSync(srcIndexHtmlPath, srcIndexHtml);
87+
this.log('changeMetaInIndex').debug('writeFileSync', srcIndexHtml);
88+
this.log('changeMetaInIndex').debug('end');
89+
return await true;
90+
} else {
91+
this.log('changeMetaInIndex').error(`File does not exists: ${srcIndexHtmlPath}`);
92+
}
93+
this.log('changeMetaInIndex').debug('end');
94+
return await false;
95+
}
3996
async changeVersion(customOptions: {}) {
4097
let rootPackagePath = path.resolve(this.rootFolder + '/package.json');
4198
let srcPackagePath = path.resolve(this.folder + '/src/app/package.json');
@@ -70,4 +127,24 @@ export class App extends Base {
70127
listComponentsPostfix: customOptions && customOptions.listComponentsPostfix ? customOptions.listComponentsPostfix : null
71128
});
72129
}
130+
async prepare(customOptions?: {
131+
i18nFolder?: string,
132+
srcFolder?: string,
133+
package?: any,
134+
listComponentsPostfix?: string
135+
rootPackagePath?: string,
136+
srcPackagePath?: string
137+
}) {
138+
this.log('prepare').debug('start');
139+
const results = [
140+
await this.extractTranslate(customOptions),
141+
await this.po2ts(customOptions),
142+
await this.extractTranslate(customOptions),
143+
await this.makeTsList(customOptions),
144+
await this.changeVersion(customOptions),
145+
await this.changeMetaInIndex(customOptions)
146+
];
147+
this.log('prepare').debug('end');
148+
return results.reduce((all: boolean, current: boolean) => { return all && current; }, true);
149+
}
73150
}

test/app-spec.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,53 @@ import * as fsExtra from 'fs-extra';
55
import * as path from 'path';
66

77
import { App } from '../src/lib/app';
8+
import * as cheerio from 'cheerio';
89

910
const assert = chai.assert;
1011
describe('App', () => {
1112
config();
1213
const debug = (process.env.TEST_DEBUG === 'true');
14+
describe('#changeMetaInIndex()', () => {
15+
const dir = path.resolve(`${__dirname}/fixture/apps/app1`);
16+
const dirRoot = path.resolve(__dirname + '/../');
17+
const srcIndexHtmlPath = path.resolve(`${dir}/src/index.html`);
18+
beforeEach(() => {
19+
let srcIndexHtml = fsExtra.readFileSync(srcIndexHtmlPath).toString();
20+
const $ = cheerio.load(srcIndexHtml);
21+
$('meta[name=version]').attr('content', '');
22+
srcIndexHtml = $.html();
23+
fsExtra.writeFileSync(srcIndexHtmlPath, srcIndexHtml);
24+
});
25+
it('changeMetaInIndex', (done) => {
26+
const app = new App(dir, dirRoot);
27+
app.debug = debug;
28+
app.changeMetaInIndex().then((data: any) => {
29+
let srcIndexHtml = fsExtra.readFileSync(srcIndexHtmlPath).toString();
30+
const $ = cheerio.load(srcIndexHtml);
31+
assert.notEqual($('meta[name=version]').attr('content'), '');
32+
done();
33+
}).catch((e: any) => {
34+
done(e);
35+
});
36+
});
37+
});
1338
describe('#makeTsList()', () => {
1439
const dir = path.resolve(`${__dirname}/fixture/apps/app1`);
1540
const dirRoot = path.resolve(__dirname + '/../');
16-
const indexTsFile = path.resolve(`${dir}/src/app/index.ts`);
41+
const srcIndexTsPath = path.resolve(`${dir}/src/app/index.ts`);
1742
beforeEach(() => {
18-
if (fsExtra.existsSync(indexTsFile)) {
19-
del.sync([indexTsFile]);
43+
if (fsExtra.existsSync(srcIndexTsPath)) {
44+
del.sync([srcIndexTsPath]);
2045
}
2146
});
22-
it(`not exists ${indexTsFile}`, () => {
23-
assert.equal(fsExtra.existsSync(indexTsFile), false);
47+
it(`not exists ${srcIndexTsPath}`, () => {
48+
assert.equal(fsExtra.existsSync(srcIndexTsPath), false);
2449
});
2550
it('srcgen -x -t make.list.ts.files -f ./srcgen/app1-make.list.ts.files.json', (done) => {
2651
const app = new App(dir, dirRoot);
2752
app.debug = debug;
2853
app.makeTsList().then((data: any) => {
29-
assert.equal(fsExtra.existsSync(indexTsFile), true);
54+
assert.equal(fsExtra.existsSync(srcIndexTsPath), true);
3055
done();
3156
}).catch((e: any) => {
3257
done(e);
@@ -82,27 +107,27 @@ describe('App', () => {
82107
const dir = path.resolve(`${__dirname}/fixture/apps/app1`);
83108
const dirRoot = path.resolve(__dirname + '/../');
84109
const translateTsFile = path.resolve(`${dir}/src/app/i18n/ru.i18n.ts`);
85-
const indexTsFile = path.resolve(`${dir}/src/app/index.ts`);
110+
const srcIndexTsPath = path.resolve(`${dir}/src/app/index.ts`);
86111
beforeEach(() => {
87112
if (fsExtra.existsSync(translateTsFile)) {
88113
del.sync([translateTsFile]);
89114
}
90-
if (fsExtra.existsSync(indexTsFile)) {
91-
del.sync([indexTsFile]);
115+
if (fsExtra.existsSync(srcIndexTsPath)) {
116+
del.sync([srcIndexTsPath]);
92117
}
93118
});
94119
it(`not exists ${translateTsFile}`, () => {
95120
assert.equal(fsExtra.existsSync(translateTsFile), false);
96121
});
97-
it(`not exists ${indexTsFile}`, () => {
98-
assert.equal(fsExtra.existsSync(indexTsFile), false);
122+
it(`not exists ${srcIndexTsPath}`, () => {
123+
assert.equal(fsExtra.existsSync(srcIndexTsPath), false);
99124
});
100125
it('npm-run-all app1:tools-extract_translate app1:tools-po2ts app1:tools-make_ts_list', (done) => {
101126
const app = new App(dir, dirRoot);
102127
app.debug = debug;
103128
app.prepare().then((data: any) => {
104129
assert.equal(fsExtra.existsSync(translateTsFile), true);
105-
assert.equal(fsExtra.existsSync(indexTsFile), true);
130+
assert.equal(fsExtra.existsSync(srcIndexTsPath), true);
106131
done();
107132
}).catch((e: any) => {
108133
done(e);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!DOCTYPE html><html><head>
2+
<meta name="version" content="1.2.4">
3+
</head>
4+
5+
<body>
6+
7+
8+
</body></html>

0 commit comments

Comments
 (0)