beforeEach(() => { fs.emptyDirSync(TEST_DIR) DIRS = dirnames.map(dir => path.join(TEST_DIR, dir)) FILES = filenames.map(f => path.join(TEST_DIR, f)) DIRS.forEach(dir => fs.ensureDirSync(dir)) FILES.forEach(f => fs.ensureFileSync(f)) })
const ensureFileExits = () => { if (!fs.existsSync(filePath)) { fs.ensureFileSync(filePath); doWriteFile([]); } }
/** * Ensure the file node exists on file system synchronously. * Wrapper for `fs.ensureFileSync`. * @returns {node} */ ensureSync() { this.fs.ensureFileSync(this.pathName); return this; }
/** * Create a `ConfigManager` instance for managing application settings * @param {String} basePath executing directory name; __dirname */ constructor(basePath = __dirname) { /** * Full path to config.json file * @type {String} */ this.configPath = resolve(basePath, 'config/config.json'); if (!existsSync(this.configPath)) { ensureFileSync(this.configPath); } }
directoryContents.forEach((componentName) => { // Ignore files if (componentName.indexOf(".") !== -1) return; // For directories, loop through each item in them const compDirectoryContents = fs.readdirSync(path.join(BASE_PATH, componentName)); compDirectoryContents.forEach((versionName) => { // Ignore anything that isn't a version directory if (!versionName.startsWith("v")) return; // In all version directories, auto-create an index.js file if there isn't one const filePath = path.join(BASE_PATH, componentName, versionName, "index.js"); fs.ensureFileSync(filePath); fs.writeFileSync(filePath, `export { default } from "./${componentName}";\n`); }); });
function generateFile (file, opts) { // ensure that the file (and its folder) exists ensureFileSync(file.absoluteName) return new Promise(resolve => { // use the appropriate generator to handle the file creation generators[file.generator](file, opts, () => { const size = `(${getFileSize(file.absoluteName)})` const type = (file.generator + ':').padEnd(13, ' ') log(`Generated ${type} ${green(file.relativeName)} ${grey(size)}`) resolve() }) }) }
it('should filter based on path', () => { const f1 = path.join(TEST_DIR, 'foo.js') const f2 = path.join(TEST_DIR, 'bar.js') fs.ensureFileSync(f1) fs.ensureFileSync(f2) const paths = [{path: f1, stats: fs.statSync(f1)}] const filterFunc = i => path.basename(i.path).indexOf('foo') > -1 const items = klawSync(TEST_DIR, {filter: filterFunc}) assert.strictEqual(items.length, paths.length) items.forEach((p, i) => { assert.deepStrictEqual(p, paths[i]) assert.strictEqual(p.path, paths[i].path) assert.deepStrictEqual(p.stats, paths[i].stats) }) })
async function initialize() { fs.ensureDirSync(`${DATA_ROOT}/imageCache`); fs.ensureFileSync(`${DATA_ROOT}/series-data.json`); fs.ensureFileSync(`${DATA_ROOT}/config.json`); seriesDataStorage = low(new FileSync(`${DATA_ROOT}/series-data.json`)); seriesDataStorage.defaults({}).write(); }
Object.values(mirrorMapper).forEach(v => { Object.values(v).forEach(async url => { const res = await fetch(url + '/index.json') const filePath = path.resolve( __dirname, '../test/__mocks__', url.replace(/^https?:\/\//, ''), 'index.json', ) fs.ensureFileSync(filePath) const stream = fs.createWriteStream(filePath).on('finish', () => { console.log('Finish:', url) }) res.body.pipe(stream) }) })
function download (url, dest, fileName) { let filePath = `${dest}/${fileName}` return new Promise(function (resolve, reject) { fs.ensureFileSync(filePath) let wget = cp.exec(`wget ${url} -O ${filePath}`) wget.stdout.pipe(process.stdout) wget.stderr.pipe(process.stderr) wget.on('close', function (code) { if (code !== 0) { console.error(`[${pkg.name}] Download error, code: ${code}`) return reject(code) } console.log(`[${pkg.name}] Download complete, wget code: ${code}.`) let currentCwd = process.cwd() process.chdir(dest) cp.execSync(`tar zxvf ${filePath}`) fs.unlinkSync(filePath) process.chdir(currentCwd) resolve(code) }) }) }
it('should filter and apply opts.nofile', () => { const f = path.join(TEST_DIR, 'foo.js') const d1 = path.join(TEST_DIR, 'foo') const d2 = path.join(TEST_DIR, 'foobar') fs.ensureFileSync(f) fs.ensureDirSync(d1) fs.ensureDirSync(d2) const paths = [ {path: d1, stats: fs.statSync(d1)}, {path: d2, stats: fs.statSync(d2)} ] const filterFunc = i => i.path.indexOf('foo') > 0 const items = klawSync(TEST_DIR, {filter: filterFunc, nofile: true}) assert.strictEqual(items.length, paths.length) items.forEach((p, i) => { assert.deepStrictEqual(p, paths[i]) assert.strictEqual(p.path, paths[i].path) assert.deepStrictEqual(p.stats, paths[i].stats) }) })
/* * Delete user from DB */ var delUser = function(username) { fs.ensureFileSync('data/user.db'); var passfile = fs.readFileSync('data/user.db', 'utf8'); var lines = passfile.split('\n'); var changed = false; var passfile_out = ''; // Re-write file without user lines.forEach(function(line) { if(line !== '') { var line_username = line.split(':')[0]; if(line_username !== username) { passfile_out += line + '\n' } else { changed = true; } } }); fs.writeFileSync('data/user.db', passfile_out); return changed; }
/** * Exclude copy from others tests for creating example test files and directories * Reason: fs-extra is not fully compatible with mock-fs module! */ describe('.copy() && .copySync()', function() { const nodePath = path.join(__dirname, '/test_dir/exmaple.node'); const copyPath = path.join(__dirname, '/test_dir/non_existent/copied.php'); const copyPath2 = path.join(__dirname, '/test_dir/copied.php'); beforeEach(function() { fs.removeSync(path.join(__dirname, 'test_dir')); fs.ensureFileSync(nodePath); fs.writeFileSync(nodePath, 'example content!', 'utf8'); }) it('.copy() && .copySync', function() { const node = new Node(nodePath); expect(node.existsSync()).to.eql(true); expect(node.copySync(copyPath)).to.eql(node); expect(node.fs.readFileSync(copyPath, 'utf8')).to.eql( 'example content!' ); return node.copy(copyPath2).then(function() { expect(node.fs.readFileSync(copyPath2, 'utf8')).to.eql( 'example content!' ); }); }); afterEach(function() { fs.removeSync(path.join(__dirname, 'test_dir')); }); });
fs.ensureFileSync('data/user.db');
directoryContents.forEach((componentName) => { // Ignore files if (componentName.indexOf(".") !== -1) return; // For directories, loop through each item in them const compDirectoryContents = fs.readdirSync(path.join(BASE_PATH, componentName)); compDirectoryContents.forEach((versionName) => { // Ignore anything that isn't a version directory if (!versionName.startsWith("v")) return; // In all version directories, auto-create an index.js file if there isn't one const filePath = path.join(BASE_PATH, componentName, versionName, "index.js"); fs.ensureFileSync(filePath); fs.writeFileSync(filePath, `export { default } from "./${componentName}";\n`); }); });