Skip to content

Commit

Permalink
Bug/issue 445 missing dependency (#447)
Browse files Browse the repository at this point in the history
* switch to native fs in test cases

* convert more fs-extra dependency usages to native fs

* convert more fs-extra dependency usages to native fs

* test bed defensive fs refactor

* test bed defensive fs refactor
  • Loading branch information
thescientist13 committed Dec 20, 2020
1 parent ae45062 commit 1a8dbb6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/data/cache.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { ApolloClient } = require('apollo-client');
const createHttpLink = require('apollo-link-http').createHttpLink;
const fetch = require('node-fetch');
const fs = require('fs-extra');
const fs = require('fs');
const { gql } = require('apollo-server');
const InMemoryCache = require('apollo-cache-inmemory').InMemoryCache;
const path = require('path');
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/lifecycles/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fs = require('fs-extra');
const fs = require('fs');
const path = require('path');
const url = require('url');

Expand All @@ -25,7 +25,7 @@ module.exports = readAndMergeConfig = async() => {
// deep clone of default config
let customConfig = Object.assign({}, defaultConfig);

if (await fs.exists(path.join(process.cwd(), 'greenwood.config.js'))) {
if (fs.existsSync(path.join(process.cwd(), 'greenwood.config.js'))) {
const userCfgFile = require(path.join(process.cwd(), 'greenwood.config.js'));
const { workspace, devServer, publicPath, title, markdown, meta } = userCfgFile;

Expand All @@ -45,7 +45,7 @@ module.exports = readAndMergeConfig = async() => {
customConfig.workspace = workspace;
}

if (!await fs.exists(customConfig.workspace)) {
if (!fs.existsSync(customConfig.workspace)) {
reject('Error: greenwood.config.js workspace doesn\'t exist! \n' +
'common issues to check might be: \n' +
'- typo in your workspace directory name, or in greenwood.config.js \n' +
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/src/lifecycles/context.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fs = require('fs-extra');
const fs = require('fs');
const path = require('path');
// const defaultTemplatesDir = path.join(__dirname, '../templates/');
// const defaultConfigDir = path.join(__dirname, '../config');
Expand Down Expand Up @@ -81,8 +81,10 @@ module.exports = initContexts = async({ config }) => {
scratchDir
};

if (!await fs.ensureDir(scratchDir)) {
await fs.mkdirs(scratchDir);
if (!fs.existsSync(scratchDir)) {
fs.mkdirSync(scratchDir, {
recursive: true
});
}

resolve(context);
Expand Down
15 changes: 5 additions & 10 deletions packages/cli/src/lifecycles/graph.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env node
const fs = require('fs-extra');
const { promises: fsp } = require('fs');
const fs = require('fs');
const crypto = require('crypto'); // TODO pretty heavy just for some hashing?
const path = require('path');
const fm = require('front-matter');
Expand All @@ -14,8 +13,8 @@ const createGraphFromPages = async (pagesDir, config) => {
const pagesIndexMap = new Map();
let pagesIndex = 0;

const walkDirectory = async(directory) => {
let files = await fs.readdir(directory);
const walkDirectory = async (directory) => {
let files = await fs.promises.readdir(directory);

return Promise.all(files.map((file) => {
const filenameHash = crypto.createHash('md5').update(`${directory}/${file}`).digest('hex');
Expand All @@ -34,7 +33,7 @@ const createGraphFromPages = async (pagesDir, config) => {
try {

if (isMdFile && !stats.isDirectory()) {
const fileContents = await fs.readFile(filePath, 'utf8');
const fileContents = await fs.promises.readFile(filePath, 'utf8');
const { attributes } = fm(fileContents);
let { label, template, title } = attributes;
let { meta } = config;
Expand Down Expand Up @@ -221,11 +220,7 @@ module.exports = generateGraph = async (compilation) => {

compilation.graph = await createGraphFromPages(context.pagesDir, config);

if (!fs.existsSync(context.scratchDir)) {
await fsp.mkdir(context.scratchDir);
}

await fsp.writeFile(`${path.join(process.cwd(), '.greenwood')}/graph.json`, JSON.stringify(compilation.graph));
await fs.promises.writeFile(`${path.join(process.cwd(), '.greenwood')}/graph.json`, JSON.stringify(compilation.graph));

resolve(compilation);
} catch (err) {
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/test/cases/eject.default/eject.default.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* User Command
* greenwood eject
*/
const fs = require('fs-extra');
const fs = require('fs');
const path = require('path');
const expect = require('chai').expect;
const TestBed = require('../../../../../test/test-bed');
Expand Down Expand Up @@ -58,7 +58,7 @@ xdescribe('Eject Greenwood With: ', function() {

configFiles.forEach(file => {
if (file !== 'eject.default.spec.js' && file !== 'node_modules') {
fs.remove(path.join(__dirname, file));
fs.rmdirSync(path.join(__dirname, file));
}
});
});
Expand Down Expand Up @@ -116,7 +116,7 @@ xdescribe('Eject Greenwood With: ', function() {

configFiles.forEach(file => {
if (file !== 'eject.default.spec.js' && file !== 'node_modules') {
fs.remove(path.join(__dirname, file));
fs.rmdirSync(path.join(__dirname, file));
}
});
});
Expand All @@ -140,7 +140,7 @@ xdescribe('Eject Greenwood With: ', function() {

configFiles.forEach(file => {
if (file !== 'eject.default.spec.js') {
fs.remove(path.join(__dirname, file));
fs.rmdirSync(path.join(__dirname, file));
}
});
});
Expand Down
24 changes: 18 additions & 6 deletions test/test-bed.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* There are a number of examples in the CLI package you can use as a reference.
*
*/
const fs = require('fs-extra');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { spawn } = require('child_process');
Expand Down Expand Up @@ -44,8 +44,10 @@ module.exports = class TestBed {

await new Promise(async(resolve, reject) => {
try {
await fs.ensureDir(targetDir, { recursive: true });
await fs.copy(targetSrc, targetPath);
await fs.promises.mkdir(targetDir, {
recursive: true
});
await fs.promises.copyFile(targetSrc, targetPath);
resolve();
} catch (err) {
reject(err);
Expand All @@ -72,12 +74,22 @@ module.exports = class TestBed {
teardownTestBed() {
return new Promise(async(resolve, reject) => {
try {
await fs.remove(path.join(this.rootDir, '.greenwood'));
await fs.remove(path.join(this.rootDir, 'public'));
if (fs.existsSync(this.buildDir)) {
await fs.promises.rmdir(this.buildDir, { recursive: true });
}

if (fs.existsSync(this.publicDir)) {
await fs.promises.rmdir(this.publicDir, { recursive: true });
}

await Promise.all(setupFiles.map((file) => {
return fs.remove(path.join(this.rootDir, file.dir.split('/')[0]));
const dir = path.join(this.rootDir, file.dir.split('/')[0]);

return fs.existsSync(dir)
? fs.promises.rmdir(dir, { recursive: true })
: Promise.resolve();
}));

resolve();
} catch (err) {
reject(err);
Expand Down

0 comments on commit 1a8dbb6

Please sign in to comment.