style: fix code style
This commit is contained in:
@@ -1,37 +1,37 @@
|
||||
import _ from 'lodash';
|
||||
|
||||
const safeVariableName = (fileName) => {
|
||||
const indexOfDot = fileName.indexOf('.');
|
||||
const indexOfDot = fileName.indexOf('.');
|
||||
|
||||
if (indexOfDot === -1) {
|
||||
return fileName;
|
||||
} else {
|
||||
return fileName.slice(0, indexOfDot);
|
||||
}
|
||||
if (indexOfDot === -1) {
|
||||
return fileName;
|
||||
} else {
|
||||
return fileName.slice(0, indexOfDot);
|
||||
}
|
||||
};
|
||||
|
||||
const buildExportBlock = (files) => {
|
||||
let importBlock;
|
||||
let importBlock;
|
||||
|
||||
importBlock = _.map(files, (fileName) => {
|
||||
return 'export ' + safeVariableName(fileName) + ' from \'./' + fileName + '\';';
|
||||
});
|
||||
importBlock = _.map(files, (fileName) => {
|
||||
return 'export ' + safeVariableName(fileName) + ' from \'./' + fileName + '\';';
|
||||
});
|
||||
|
||||
importBlock = importBlock.join('\n');
|
||||
importBlock = importBlock.join('\n');
|
||||
|
||||
return importBlock;
|
||||
return importBlock;
|
||||
};
|
||||
|
||||
export default (filePaths) => {
|
||||
let code;
|
||||
let code;
|
||||
|
||||
code = '\'create index\';\n\n';
|
||||
code = '\'create index\';\n\n';
|
||||
|
||||
if (filePaths.length) {
|
||||
const sortedFilePaths = filePaths.sort();
|
||||
if (filePaths.length) {
|
||||
const sortedFilePaths = filePaths.sort();
|
||||
|
||||
code += buildExportBlock(sortedFilePaths) + '\n\n';
|
||||
}
|
||||
code += buildExportBlock(sortedFilePaths) + '\n\n';
|
||||
}
|
||||
|
||||
return code;
|
||||
return code;
|
||||
};
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
import path from 'path';
|
||||
import _ from 'lodash';
|
||||
import glob from 'glob';
|
||||
import path from 'path';
|
||||
import validateTargetDirectory from './validateTargetDirectory';
|
||||
|
||||
export default (directoryPath) => {
|
||||
let targetDirectories;
|
||||
let targetDirectories;
|
||||
|
||||
targetDirectories = glob.sync(path.join(directoryPath, './**/index.js'));
|
||||
targetDirectories = glob.sync(path.join(directoryPath, './**/index.js'));
|
||||
|
||||
targetDirectories = _.filter(targetDirectories, (targetDirectoryPath) => {
|
||||
try {
|
||||
validateTargetDirectory(path.dirname(targetDirectoryPath));
|
||||
targetDirectories = _.filter(targetDirectories, (targetDirectoryPath) => {
|
||||
try {
|
||||
validateTargetDirectory(path.dirname(targetDirectoryPath));
|
||||
|
||||
return true;
|
||||
/* eslint-disable no-empty */
|
||||
} catch (error) {
|
||||
return true;
|
||||
|
||||
}
|
||||
/* eslint-enable no-empty */
|
||||
// eslint-disable-next-line no-empty
|
||||
} catch (error) {
|
||||
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
targetDirectories = _.map(targetDirectories, path.dirname);
|
||||
return false;
|
||||
});
|
||||
|
||||
return targetDirectories;
|
||||
targetDirectories = _.map(targetDirectories, path.dirname);
|
||||
|
||||
return targetDirectories;
|
||||
};
|
||||
|
||||
@@ -3,6 +3,7 @@ import moment from 'moment';
|
||||
|
||||
export default (...append) => {
|
||||
/* eslint-disable no-console */
|
||||
console.log(chalk.dim('[' + moment().format('HH:mm:ss') + ']'), ...append);
|
||||
console.log(chalk.dim('[' + moment().format('HH:mm:ss') + ']'), ...append);
|
||||
|
||||
/* eslint-enable no-console */
|
||||
};
|
||||
|
||||
@@ -1,81 +1,81 @@
|
||||
import fs from 'fs';
|
||||
import _ from 'lodash';
|
||||
import path from 'path';
|
||||
import _ from 'lodash';
|
||||
import validateTargetDirectory from './validateTargetDirectory';
|
||||
|
||||
const hasIndex = (directoryPath) => {
|
||||
const indexPath = path.resolve(directoryPath, 'index.js');
|
||||
const indexPath = path.resolve(directoryPath, 'index.js');
|
||||
|
||||
try {
|
||||
fs.statSync(indexPath);
|
||||
try {
|
||||
fs.statSync(indexPath);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const hasNoExtension = (fileName) => {
|
||||
const matches = fileName.match(/\./g);
|
||||
const matches = fileName.match(/\./g);
|
||||
|
||||
return !matches;
|
||||
return !matches;
|
||||
};
|
||||
|
||||
const hasMultipleExtensions = (fileName) => {
|
||||
const matches = fileName.match(/\./g);
|
||||
const matches = fileName.match(/\./g);
|
||||
|
||||
return matches && matches.length > 1;
|
||||
return matches && matches.length > 1;
|
||||
};
|
||||
|
||||
const isSafeName = (fileName) => {
|
||||
return /^[a-z][a-z0-9\._]+$/i.test(fileName);
|
||||
return /^[a-z][a-z0-9._]+$/i.test(fileName);
|
||||
};
|
||||
|
||||
const removeDuplicates = (files) => {
|
||||
return _.filter(files, (fileName) => {
|
||||
return !_.includes(files, fileName + '.js');
|
||||
});
|
||||
return _.filter(files, (fileName) => {
|
||||
return !_.includes(files, fileName + '.js');
|
||||
});
|
||||
};
|
||||
|
||||
export default (directoryPath) => {
|
||||
let children;
|
||||
let children;
|
||||
|
||||
validateTargetDirectory(directoryPath);
|
||||
validateTargetDirectory(directoryPath);
|
||||
|
||||
children = fs.readdirSync(directoryPath);
|
||||
children = fs.readdirSync(directoryPath);
|
||||
|
||||
children = _.filter(children, (fileName) => {
|
||||
const absolutePath = path.resolve(directoryPath, fileName);
|
||||
const isDirectory = fs.statSync(absolutePath).isDirectory();
|
||||
children = _.filter(children, (fileName) => {
|
||||
const absolutePath = path.resolve(directoryPath, fileName);
|
||||
const isDirectory = fs.statSync(absolutePath).isDirectory();
|
||||
|
||||
if (!isSafeName(fileName)) {
|
||||
return false;
|
||||
}
|
||||
if (!isSafeName(fileName)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (hasNoExtension(fileName) && !isDirectory) {
|
||||
return false;
|
||||
}
|
||||
if (hasNoExtension(fileName) && !isDirectory) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (hasMultipleExtensions(fileName)) {
|
||||
return false;
|
||||
}
|
||||
if (hasMultipleExtensions(fileName)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_.startsWith(fileName, 'index.js')) {
|
||||
return false;
|
||||
}
|
||||
if (_.startsWith(fileName, 'index.js')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!isDirectory && !_.endsWith(fileName, '.js')) {
|
||||
return false;
|
||||
}
|
||||
if (!isDirectory && !_.endsWith(fileName, '.js')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isDirectory && !hasIndex(absolutePath)) {
|
||||
return false;
|
||||
}
|
||||
if (isDirectory && !hasIndex(absolutePath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
return true;
|
||||
});
|
||||
|
||||
children = removeDuplicates(children);
|
||||
children = removeDuplicates(children);
|
||||
|
||||
return children.sort();
|
||||
return children.sort();
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import _ from 'lodash';
|
||||
|
||||
export default (paths) => {
|
||||
return _.sortBy(paths, (path) => {
|
||||
return -path.split('/').length;
|
||||
});
|
||||
return _.sortBy(paths, (path) => {
|
||||
return -path.split('/').length;
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,33 +1,33 @@
|
||||
import _ from 'lodash';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import _ from 'lodash';
|
||||
|
||||
export default (targetDirectory) => {
|
||||
let stats;
|
||||
let stats;
|
||||
|
||||
try {
|
||||
stats = fs.statSync(targetDirectory);
|
||||
} catch (error) {
|
||||
throw new Error('Directory "' + targetDirectory + '" does not exist.');
|
||||
}
|
||||
try {
|
||||
stats = fs.statSync(targetDirectory);
|
||||
} catch (error) {
|
||||
throw new Error('Directory "' + targetDirectory + '" does not exist.');
|
||||
}
|
||||
|
||||
if (!stats.isDirectory()) {
|
||||
throw new Error('"' + targetDirectory + '" is not a directory.');
|
||||
}
|
||||
if (!stats.isDirectory()) {
|
||||
throw new Error('"' + targetDirectory + '" is not a directory.');
|
||||
}
|
||||
|
||||
const indexFilePath = path.resolve(targetDirectory, './index.js');
|
||||
|
||||
try {
|
||||
fs.statSync(indexFilePath);
|
||||
} catch (error) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const indexFile = fs.readFileSync(indexFilePath, 'utf8');
|
||||
|
||||
if (!_.startsWith(indexFile, '\'create index\';\n')) {
|
||||
throw new Error('"' + indexFilePath + '" unsafe index.');
|
||||
}
|
||||
const indexFilePath = path.resolve(targetDirectory, './index.js');
|
||||
|
||||
try {
|
||||
fs.statSync(indexFilePath);
|
||||
} catch (error) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const indexFile = fs.readFileSync(indexFilePath, 'utf8');
|
||||
|
||||
if (!_.startsWith(indexFile, '\'create index\';\n')) {
|
||||
throw new Error('"' + indexFilePath + '" unsafe index.');
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
+12
-12
@@ -1,23 +1,23 @@
|
||||
import _ from 'lodash';
|
||||
import createIndexCode from './createIndexCode';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import _ from 'lodash';
|
||||
import createIndexCode from './createIndexCode';
|
||||
import validateTargetDirectory from './validateTargetDirectory';
|
||||
import readDirectory from './readDirectory';
|
||||
import sortByDepth from './sortByDepth';
|
||||
|
||||
export default (directoryPaths) => {
|
||||
const sortedDirectoryPaths = sortByDepth(directoryPaths);
|
||||
const sortedDirectoryPaths = sortByDepth(directoryPaths);
|
||||
|
||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||
validateTargetDirectory(directoryPath);
|
||||
});
|
||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||
validateTargetDirectory(directoryPath);
|
||||
});
|
||||
|
||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||
const siblings = readDirectory(directoryPath);
|
||||
const indexCode = createIndexCode(siblings);
|
||||
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||
const siblings = readDirectory(directoryPath);
|
||||
const indexCode = createIndexCode(siblings);
|
||||
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
||||
|
||||
fs.writeFileSync(indexFilePath, indexCode);
|
||||
});
|
||||
fs.writeFileSync(indexFilePath, indexCode);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,62 +1,64 @@
|
||||
import _ from 'lodash';
|
||||
import createIndexCode from './createIndexCode';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import _ from 'lodash';
|
||||
import chalk from 'chalk';
|
||||
import createIndexCode from './createIndexCode';
|
||||
import validateTargetDirectory from './validateTargetDirectory';
|
||||
import readDirectory from './readDirectory';
|
||||
import sortByDepth from './sortByDepth';
|
||||
import log from './log';
|
||||
import findIndexFiles from './findIndexFiles';
|
||||
import chalk from 'chalk';
|
||||
|
||||
export default (directoryPaths, options = {}) => {
|
||||
let sortedDirectoryPaths;
|
||||
let sortedDirectoryPaths;
|
||||
|
||||
sortedDirectoryPaths = sortByDepth(directoryPaths);
|
||||
sortedDirectoryPaths = sortByDepth(directoryPaths);
|
||||
|
||||
log('Target directories', sortedDirectoryPaths);
|
||||
log('Update index:', options.updateIndex ? chalk.green('true') : chalk.red('false'));
|
||||
log('Target directories', sortedDirectoryPaths);
|
||||
log('Update index:', options.updateIndex ? chalk.green('true') : chalk.red('false'));
|
||||
|
||||
if (options.updateIndex) {
|
||||
sortedDirectoryPaths = _.map(sortedDirectoryPaths, findIndexFiles);
|
||||
sortedDirectoryPaths = _.flatten(sortedDirectoryPaths);
|
||||
sortedDirectoryPaths = _.uniq(sortedDirectoryPaths);
|
||||
sortedDirectoryPaths = sortByDepth(sortedDirectoryPaths);
|
||||
if (options.updateIndex) {
|
||||
sortedDirectoryPaths = _.map(sortedDirectoryPaths, findIndexFiles);
|
||||
sortedDirectoryPaths = _.flatten(sortedDirectoryPaths);
|
||||
sortedDirectoryPaths = _.uniq(sortedDirectoryPaths);
|
||||
sortedDirectoryPaths = sortByDepth(sortedDirectoryPaths);
|
||||
|
||||
log('Found index file in:', sortedDirectoryPaths);
|
||||
}
|
||||
|
||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||
validateTargetDirectory(directoryPath);
|
||||
});
|
||||
|
||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||
let existingIndexCode;
|
||||
|
||||
const siblings = readDirectory(directoryPath);
|
||||
|
||||
const indexCode = createIndexCode(siblings);
|
||||
|
||||
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
||||
|
||||
try {
|
||||
existingIndexCode = fs.readFileSync(indexFilePath, 'utf8');
|
||||
|
||||
/* eslint-disable no-empty */
|
||||
} catch (error) {
|
||||
|
||||
log('Found index file in:', sortedDirectoryPaths);
|
||||
}
|
||||
|
||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||
validateTargetDirectory(directoryPath);
|
||||
});
|
||||
|
||||
_.forEach(sortedDirectoryPaths, (directoryPath) => {
|
||||
let existingIndexCode;
|
||||
|
||||
const siblings = readDirectory(directoryPath);
|
||||
|
||||
const indexCode = createIndexCode(siblings);
|
||||
|
||||
const indexFilePath = path.resolve(directoryPath, 'index.js');
|
||||
|
||||
try {
|
||||
existingIndexCode = fs.readFileSync(indexFilePath, 'utf8');
|
||||
/* eslint-disable no-empty */
|
||||
} catch (error) {
|
||||
|
||||
}
|
||||
/* eslint-enable no-empty */
|
||||
|
||||
fs.writeFileSync(indexFilePath, indexCode);
|
||||
fs.writeFileSync(indexFilePath, indexCode);
|
||||
|
||||
if (existingIndexCode && existingIndexCode === indexCode) {
|
||||
log(indexFilePath, chalk.yellow('[index have not changed]'));
|
||||
} else if (existingIndexCode && existingIndexCode !== indexCode) {
|
||||
log(indexFilePath, chalk.green('[updated index]'));
|
||||
} else {
|
||||
log(indexFilePath, chalk.green('[created index]'));
|
||||
}
|
||||
});
|
||||
if (existingIndexCode && existingIndexCode === indexCode) {
|
||||
log(indexFilePath, chalk.yellow('[index have not changed]'));
|
||||
} else if (existingIndexCode && existingIndexCode !== indexCode) {
|
||||
log(indexFilePath, chalk.green('[updated index]'));
|
||||
} else {
|
||||
log(indexFilePath, chalk.green('[created index]'));
|
||||
}
|
||||
});
|
||||
|
||||
log('Done');
|
||||
log('Done');
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user