Skip to content

Commit bcab218

Browse files
author
MaxGenash
committed
refactor: (strf-8608) make fsUtils async
1 parent fdcdd82 commit bcab218

15 files changed

Lines changed: 248 additions & 363 deletions

bin/stencil-bundle.js

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const program = require('../lib/commander');
66
const { THEME_PATH, PACKAGE_INFO } = require('../constants');
77
const ThemeConfig = require('../lib/theme-config');
88
const Bundle = require('../lib/stencil-bundle');
9+
const { printCliResultErrorAndExit } = require('../lib/cliCommon');
910
const { checkNodeVersion } = require('../lib/cliCommon');
1011

1112
program
@@ -27,29 +28,35 @@ program
2728
const cliOptions = program.opts();
2829
const themeConfig = ThemeConfig.getInstance(THEME_PATH);
2930

30-
checkNodeVersion();
31+
async function run() {
32+
try {
33+
checkNodeVersion();
3134

32-
if (cliOptions.dest === true) {
33-
throw new Error('You have to specify a value for -d or --dest'.red);
34-
}
35+
if (cliOptions.dest === true) {
36+
throw new Error('You have to specify a value for -d or --dest'.red);
37+
}
3538

36-
if (cliOptions.name === true) {
37-
throw new Error('You have to specify a value for -n or --name'.red);
38-
}
39+
if (cliOptions.name === true) {
40+
throw new Error('You have to specify a value for -n or --name'.red);
41+
}
3942

40-
if (!themeConfig.configExists()) {
41-
throw new Error(
42-
`${'You must have a '.red + 'config.json'.cyan} file in your top level theme directory.`,
43-
);
44-
}
43+
if (!themeConfig.configExists()) {
44+
throw new Error(
45+
`${
46+
'You must have a '.red + 'config.json'.cyan
47+
} file in your top level theme directory.`,
48+
);
49+
}
4550

46-
const rawConfig = themeConfig.getRawConfig();
47-
const bundle = new Bundle(THEME_PATH, themeConfig, rawConfig, cliOptions);
51+
const rawConfig = await themeConfig.getRawConfig();
52+
const bundle = new Bundle(THEME_PATH, themeConfig, rawConfig, cliOptions);
4853

49-
bundle.initBundle((err, bundlePath) => {
50-
if (err) {
51-
throw err;
54+
const bundlePath = await bundle.initBundle();
55+
56+
console.log(`Bundled saved to: ${bundlePath.cyan}`);
57+
} catch (err) {
58+
printCliResultErrorAndExit(err);
5259
}
60+
}
5361

54-
console.log(`Bundled saved to: ${bundlePath.cyan}`);
55-
});
62+
run();

lib/bundle-validator.js

Lines changed: 43 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -117,40 +117,29 @@ class BundleValidator {
117117
/**
118118
* If theme schema exists we need to validate it to make sure it passes all defined checks.
119119
* @private
120-
* @param callback
121-
* @returns {void}
120+
* @returns {boolean}
122121
*/
123-
_validateThemeSchema(callback) {
122+
async _validateThemeSchema() {
124123
if (this.themeConfig.schemaExists()) {
125-
try {
126-
this._validateJsonSchema(
127-
'schema',
128-
themeValidationSchema,
129-
this.themeConfig.getRawSchema(),
130-
);
131-
} catch (error) {
132-
callback(error);
133-
return;
134-
}
124+
const rawSchema = await this.themeConfig.getRawSchema();
125+
this._validateJsonSchema('schema', themeValidationSchema, rawSchema);
135126
}
136127

137-
callback(null, true);
128+
return true;
138129
}
139130

140131
/**
141132
* Ensure theme configuration exists and passes the json schema file
142133
* @private
143-
* @param callback
144-
* @returns {void}
134+
* @returns {boolean}
145135
*/
146-
_validateThemeConfiguration(callback) {
136+
async _validateThemeConfiguration() {
147137
if (!this.themeConfig.configExists()) {
148138
const errMsg =
149139
'You must have a '.red +
150140
'config.json'.cyan +
151141
' file in your top level theme directory.';
152-
callback(new Error(errMsg));
153-
return;
142+
throw new Error(errMsg);
154143
}
155144

156145
if (!this.isPrivate && !this.themeConfig.schemaExists()) {
@@ -161,22 +150,17 @@ class BundleValidator {
161150
const validationSchema = this.isPrivate
162151
? privateThemeConfigValidationSchema
163152
: themeConfigValidationSchema;
164-
try {
165-
this._validateJsonSchema('config', validationSchema, this.themeConfig.getRawConfig());
166-
} catch (error) {
167-
callback(error);
168-
return;
169-
}
153+
const rawConfig = await this.themeConfig.getRawConfig();
170154

171-
callback(null, true);
155+
return this._validateJsonSchema('config', validationSchema, rawConfig);
172156
}
173157

174158
/**
175159
* @private
176160
* @param type
177161
* @param schema
178162
* @param data
179-
* @returns {void}
163+
* @returns {boolean}
180164
*/
181165
_validateJsonSchema(type, schema, data) {
182166
const validator = new Validator(this.jsonSchemaValidatorOptions);
@@ -189,33 +173,32 @@ class BundleValidator {
189173
}
190174
throw new Error(errorMessage.red);
191175
}
176+
177+
return true;
192178
}
193179

194180
/**
195181
* Ensure that schema translations exists and there are no missing or unused keys.
196182
* @private
197-
* @param {function} callback
198-
* @returns {void}
183+
* @returns {boolean}
199184
*/
200-
_validateSchemaTranslations(callback) {
185+
async _validateSchemaTranslations() {
201186
const validatorSchemaTranslations = new ValidatorSchemaTranslations();
202187
const validator = new Validator(this.jsonSchemaValidatorOptions);
203188

204189
if (this.themeConfig.schemaExists()) {
205-
validatorSchemaTranslations.setSchema(this.themeConfig.getRawSchema());
190+
validatorSchemaTranslations.setSchema(await this.themeConfig.getRawSchema());
206191
}
207192

208193
if (this.themeConfig.schemaTranslationsExists()) {
209194
try {
210-
validatorSchemaTranslations.setTranslations(
211-
this.themeConfig.getRawSchemaTranslations(),
212-
);
195+
const rawSchemaTranslations = await this.themeConfig.getRawSchemaTranslations();
196+
validatorSchemaTranslations.setTranslations(rawSchemaTranslations);
213197
} catch (e) {
214198
throw new Error('Corrupted schemaTranslations.json file'.red);
215199
}
216200
} else if (validatorSchemaTranslations.getSchemaKeys().length) {
217-
callback(new Error('Missed schemaTranslations.json file'.red));
218-
return;
201+
throw new Error('Missed schemaTranslations.json file'.red);
219202
}
220203

221204
const missedKeys = validatorSchemaTranslations.findMissedKeys();
@@ -246,36 +229,32 @@ class BundleValidator {
246229
});
247230
}
248231

249-
callback(new Error(errorMessage.red));
250-
return;
232+
throw new Error(errorMessage.red);
251233
}
252234

253-
callback(null, true);
235+
return true;
254236
}
255237

256238
/**
257239
* Validates images for marketplace themes
258240
* @private
259-
* @param callback
260-
* @returns {void}
241+
* @returns {boolean[]}
261242
*/
262-
_validateMetaImages(callback) {
263-
const { meta, variations } = this.themeConfig.getConfig();
243+
async _validateMetaImages() {
244+
const { meta, variations } = await this.themeConfig.getConfig();
264245
const composedImagePath = path.resolve(this.themePath, 'meta', meta.composed_image);
265246
const imageTasks = [];
266247

267248
if (!this._isValidImageType(composedImagePath)) {
268-
const errorMsg =
249+
throw new Error(
269250
'Invalid file type for "meta.composed_image".'.red +
270-
`\r\nValid types (${VALID_IMAGE_TYPES.join(', ')})`.red;
271-
callback(new Error(errorMsg));
272-
return;
251+
`\r\nValid types (${VALID_IMAGE_TYPES.join(', ')})`.red,
252+
);
273253
}
274254
if (!fs.existsSync(composedImagePath)) {
275-
const errorMsg = 'The path you specified for your "meta.composed_image" does not exist.'
276-
.red;
277-
callback(new Error(errorMsg));
278-
return;
255+
throw new Error(
256+
'The path you specified for your "meta.composed_image" does not exist.'.red,
257+
);
279258
}
280259
imageTasks.push((cb) =>
281260
this._validateImage(composedImagePath, WIDTH_COMPOSED, HEIGHT_COMPOSED, cb),
@@ -291,17 +270,15 @@ class BundleValidator {
291270
);
292271

293272
if (!this._isValidImageType(desktopScreenshotPath)) {
294-
const errorMsg =
273+
throw new Error(
295274
`Invalid file type for ${id} variation's "desktop_screenshot".`.red +
296-
`\r\nValid types (${VALID_IMAGE_TYPES.join(', ')})`.red;
297-
callback(new Error(errorMsg));
298-
return;
275+
`\r\nValid types (${VALID_IMAGE_TYPES.join(', ')})`.red,
276+
);
299277
}
300278
if (!fs.existsSync(desktopScreenshotPath)) {
301-
const errorMsg = `The path you specified for the ${id} variation's "desktop_screenshot" does not exist.`
302-
.red;
303-
callback(new Error(errorMsg));
304-
return;
279+
throw new Error(
280+
`The path you specified for the ${id} variation's "desktop_screenshot" does not exist.`.red,
281+
);
305282
}
306283
imageTasks.push((cb) =>
307284
this._validateImage(desktopScreenshotPath, WIDTH_DESKTOP, HEIGHT_DESKTOP, cb),
@@ -314,26 +291,22 @@ class BundleValidator {
314291
);
315292

316293
if (!this._isValidImageType(mobileScreenshotPath)) {
317-
const errorMsg =
294+
throw new Error(
318295
`Invalid file type for ${id} variation's "mobile_screenshot".`.red +
319-
`\r\nValid types (${VALID_IMAGE_TYPES.join(', ')})`.red;
320-
callback(new Error(errorMsg));
321-
return;
296+
`\r\nValid types (${VALID_IMAGE_TYPES.join(', ')})`.red,
297+
);
322298
}
323299
if (!fs.existsSync(mobileScreenshotPath)) {
324-
const errorMsg = `The path you specified for the ${id} variation's "mobile_screenshot" does not exist.`
325-
.red;
326-
callback(new Error(errorMsg));
327-
return;
300+
throw new Error(
301+
`The path you specified for the ${id} variation's "mobile_screenshot" does not exist.`.red,
302+
);
328303
}
329304
imageTasks.push((cb) =>
330305
this._validateImage(mobileScreenshotPath, WIDTH_MOBILE, HEIGHT_MOBILE, cb),
331306
);
332307
}
333308

334-
async.parallel(imageTasks, (err, result) => {
335-
callback(err, result);
336-
});
309+
return async.parallel(imageTasks);
337310
}
338311

339312
/**

lib/bundle-validator.spec.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ const themePath = path.join(process.cwd(), 'test/_mocks/themes/valid');
99
describe('BundleValidator', () => {
1010
let themeConfig;
1111

12-
beforeEach(() => {
12+
beforeEach(async () => {
1313
themeConfig = ThemeConfig.getInstance(themePath);
14-
themeConfig.getConfig();
1514
});
1615

1716
afterEach(() => {
@@ -109,7 +108,6 @@ describe('BundleValidator', () => {
109108
it('should validate theme schema and throw errors', async () => {
110109
const themePath2 = path.join(process.cwd(), 'test/_mocks/themes/invalid-schema');
111110
themeConfig = ThemeConfig.getInstance(themePath2);
112-
themeConfig.getConfig();
113111

114112
const validator = new BundleValidator(themePath2, themeConfig, false);
115113

lib/regions.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ describe('Stencil Bundle', () => {
1212
beforeEach(() => {
1313
const themeConfigStub = {
1414
configExists: jest.fn().mockReturnValue(true),
15-
getRawConfig: jest.fn().mockReturnValue({}),
16-
getSchema: jest.fn().mockImplementation((cb) => cb(null)),
15+
getRawConfig: jest.fn().mockResolvedValue({}),
16+
getSchema: jest.fn().mockResolvedValue(null),
1717
};
1818

1919
const rawConfig = {

lib/release/questions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async function askQuestions(themeConfig, githubToken, remotes) {
1212
return { value: remote, name: `${remote.name}: ${remote.url}` };
1313
});
1414

15-
const currentVersion = themeConfig.getVersion();
15+
const currentVersion = await themeConfig.getVersion();
1616
const nextPatchVersion = semver.inc(currentVersion, 'patch');
1717
const nextMinorVersion = semver.inc(currentVersion, 'minor');
1818
const nextMajorVersion = semver.inc(currentVersion, 'major');

0 commit comments

Comments
 (0)