Skip to content

Commit

Permalink
refactor: use fs.existsSync instead of Fs.statSync hack
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGenash committed Sep 14, 2020
1 parent fc62b19 commit e097e36
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 38 deletions.
18 changes: 2 additions & 16 deletions bin/stencil-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ if (!versionCheck()) {
process.exit(2);
}

if (!fileExist(dotStencilFilePath)) {
if (!Fs.existsSync(dotStencilFilePath)) {
console.error('Error: Please run'.red + ' $ stencil init'.cyan + ' first.'.red);
process.exit(2);
}

if (!fileExist(Path.join(themePath, 'config.json'))) {
if (!Fs.existsSync(Path.join(themePath, 'config.json'))) {
console.error('Error: You must have a '.red + 'config.json'.cyan + ' file in your top level theme directory.');
process.exit(2);
}
Expand Down Expand Up @@ -244,20 +244,6 @@ function assembleTemplates(templatePath, callback) {
});
}

/**
* Check if file exist synchronous
* @param {string} path
* @return {boolean}
*/
function fileExist(path) {
try {
return !!Fs.statSync(path);
}
catch (e) {
return false;
}
}

/**
* Displays information about your environment and configuration.
* @return {string}
Expand Down
20 changes: 3 additions & 17 deletions lib/theme-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,23 +279,23 @@ ThemeConfig.prototype.getDemoUrl = function () {
* @return {Boolean}
*/
ThemeConfig.prototype.configExists = function () {
return fileExists(this.configPath);
return Fs.existsSync(this.configPath);
};

/**
* Check if the schema.json file exists
* @return {Boolean}
*/
ThemeConfig.prototype.schemaExists = function () {
return fileExists(this.schemaPath);
return Fs.existsSync(this.schemaPath);
};

/**
* Check if the schemaTranslations.json file exists
* @return {Boolean}
*/
ThemeConfig.prototype.schemaTranslationsExists = function () {
return fileExists(this.schemaTranslationsPath);
return Fs.existsSync(this.schemaTranslationsPath);
};

/**
Expand Down Expand Up @@ -477,17 +477,3 @@ function getVariation(config, variationIndex) {

return variation;
}

/**
* Check if file exist syncronous
* @param {string} path
* @return {boolean}
*/
function fileExists(path) {
try {
return !!Fs.statSync(path);
}
catch (e) {
return false;
}
}
4 changes: 1 addition & 3 deletions tasks/changelog/changelog-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ function ChangelogGenerator(fs, cwd, commandExecutor) {
...customOptions,
};

try {
fs.statSync(options.infile);
} catch(error) {
if (!fs.existsSync(options.infile)) {
options.releaseCount = 0;
}

Expand Down
7 changes: 5 additions & 2 deletions tasks/changelog/changelog-generator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('ChangelogGenerator', () => {

beforeEach( () => {
fsMock = {
statSync: function() { return true; },
existsSync: () => true,
};

commandExecutor = new CommandExecutor(require('child_process'));
Expand Down Expand Up @@ -70,7 +70,10 @@ describe('ChangelogGenerator', () => {
});

it('executes `conventional-changelog` command from scratch if CHANGELOG does not exist', async() => {
const changelogGeneratorWithoutFs = new ChangelogGenerator({}, '/src', commandExecutor);
const fsMock = {
existsSync: () => false,
};
const changelogGeneratorWithoutFs = new ChangelogGenerator(fsMock, '/src', commandExecutor);

try {
await promisify(changelogGeneratorWithoutFs.generateChangelog.bind(changelogGeneratorWithoutFs))({});
Expand Down

0 comments on commit e097e36

Please sign in to comment.