Change the index file format to use "export ... from" (fixes #3)

This commit is contained in:
Gajus Kuizinas
2016-01-29 09:40:35 +00:00
parent 832044c9fd
commit 69825b59c6
14 changed files with 35 additions and 68 deletions
+43
View File
@@ -0,0 +1,43 @@
import _ from 'lodash';
let buildExportBlock,
safeVariableName;
safeVariableName = (fileName) => {
let indexOfDot;
indexOfDot = fileName.indexOf('.');
if (indexOfDot === -1) {
return fileName;
} else {
return fileName.slice(0, indexOfDot);
}
};
buildExportBlock = (files) => {
let importBlock;
importBlock = _.map(files, (fileName) => {
return 'export ' + safeVariableName(fileName) + ' from \'./' + fileName + '\';';
});
importBlock = importBlock.join('\n');
return importBlock;
};
export default (filePaths) => {
let code,
sortedFilePaths;
code = '\'create index\';\n\n';
if (filePaths.length) {
sortedFilePaths = filePaths.sort();
code += buildExportBlock(sortedFilePaths) + '\n\n';
}
return code;
};
+8
View File
@@ -0,0 +1,8 @@
'create index';
export createIndexCode from './createIndexCode.js';
export readDirectory from './readDirectory.js';
export sortByDepth from './sortByDepth.js';
export validateTargetDirectory from './validateTargetDirectory.js';
export writeIndex from './writeIndex.js';
+97
View File
@@ -0,0 +1,97 @@
import fs from 'fs';
import _ from 'lodash';
import path from 'path';
import validateTargetDirectory from './validateTargetDirectory';
let hasIndex,
hasMultipleExtensions,
hasNoExtension,
isSafeName,
removeDuplicates;
hasIndex = (directoryPath) => {
let indexPath;
indexPath = path.resolve(directoryPath, 'index.js');
try {
fs.statSync(indexPath);
return true;
} catch (error) {
return false;
}
};
hasNoExtension = (fileName) => {
let matches;
matches = fileName.match(/\./g);
return !matches;
};
hasMultipleExtensions = (fileName) => {
let matches;
matches = fileName.match(/\./g);
return matches && matches.length > 1;
};
isSafeName = (fileName) => {
return /^[a-z][a-z0-9\.]+$/i.test(fileName);
};
removeDuplicates = (files) => {
return _.filter(files, (fileName) => {
return !_.includes(files, fileName + '.js');
});
};
export default (directoryPath) => {
let children;
validateTargetDirectory(directoryPath);
children = fs.readdirSync(directoryPath);
children = _.filter(children, (fileName) => {
let absolutePath,
isDirectory;
absolutePath = path.resolve(directoryPath, fileName);
isDirectory = fs.statSync(absolutePath).isDirectory();
if (!isSafeName(fileName)) {
return false;
}
if (hasNoExtension(fileName) && !isDirectory) {
return false;
}
if (hasMultipleExtensions(fileName)) {
return false;
}
if (_.startsWith(fileName, 'index.js')) {
return false;
}
if (!isDirectory && !_.endsWith(fileName, '.js')) {
return false;
}
if (isDirectory && !hasIndex(absolutePath)) {
return false;
}
return true;
});
children = removeDuplicates(children);
return children.sort();
};
+7
View File
@@ -0,0 +1,7 @@
import _ from 'lodash';
export default (paths) => {
return _.sortBy(paths, (path) => {
return -path.split('/').length;
});
};
+35
View File
@@ -0,0 +1,35 @@
import _ from 'lodash';
import fs from 'fs';
import path from 'path';
export default (targetDirectory) => {
let indexFile,
indexFilePath,
stats;
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.');
}
indexFilePath = path.resolve(targetDirectory, './index.js');
try {
fs.statSync(indexFilePath);
} catch (error) {
return true;
}
indexFile = fs.readFileSync(indexFilePath, 'utf8');
if (!_.startsWith(indexFile, '\'create index\';\n')) {
throw new Error('"' + indexFilePath + '" unsafe index.');
}
return true;
};
+31
View File
@@ -0,0 +1,31 @@
import _ from 'lodash';
import createIndexCode from './createIndexCode';
import fs from 'fs';
import path from 'path';
import validateTargetDirectory from './validateTargetDirectory';
import readDirectory from './readDirectory';
import sortByDepth from './sortByDepth';
export default (directoryPaths) => {
let sortedDirectoryPaths;
sortedDirectoryPaths = sortByDepth(directoryPaths);
_.forEach(sortedDirectoryPaths, (directoryPath) => {
validateTargetDirectory(directoryPath);
});
_.forEach(sortedDirectoryPaths, (directoryPath) => {
let indexCode,
indexFilePath,
siblings;
siblings = readDirectory(directoryPath);
indexCode = createIndexCode(siblings);
indexFilePath = path.resolve(directoryPath, 'index.js');
fs.writeFileSync(indexFilePath, indexCode);
});
};