index-tasukaru
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -23,3 +23,2 @@ "use strict"; | ||
// TODO: support custom output | ||
// TODO: case insensitive | ||
// TODO: add tests | ||
@@ -42,13 +41,28 @@ // TODO: allow targetting .js | ||
let targetDir = (_a = argv._[0]) !== null && _a !== void 0 ? _a : '.'; | ||
let matchedFiles = yield writeIndexForDir(targetDir, splitSourceExtsArg(String(argv.sourceExts).trim()), Boolean(argv.f)); | ||
echo(`Finished exporting ${matchedFiles.length} file(s).`); | ||
}); | ||
} | ||
exports.main = main; | ||
function writeIndexForDir(targetDir, sourceExts, force) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let workDir = path_1.default.join(process.cwd(), targetDir); | ||
let files = yield fs_extra_1.default.readdir(workDir); | ||
let files = []; | ||
try { | ||
files = yield fs_extra_1.default.readdir(workDir); | ||
} | ||
catch (error) { | ||
if (error.code === 'ENOENT') { | ||
exitWithError(`\`${targetDir}\` directory does not exist.`, 3); | ||
} | ||
else { | ||
exitWithError(`Failed to read \`${targetDir}\`.`, 4); | ||
} | ||
} | ||
let targetIndex = 'index.ts'; | ||
let targetIndexPath = path_1.default.join(workDir, targetIndex); | ||
let targetIndexPathRelative = path_1.default.join(targetDir, targetIndex); | ||
let force = Boolean(argv.f); | ||
if (!force && files.includes(targetIndex)) { | ||
// TODO: Add confirmation instead of exit. | ||
exitWithError(`\`${targetIndexPathRelative}\` already exist.\nUse -f to force writing the index file.`, 1); | ||
} | ||
let sourceExts = splitSourceExtsArg(String(argv.sourceExts).trim()); | ||
let matchedFiles = files | ||
@@ -64,6 +78,5 @@ // Skip dotfiles and target file | ||
yield fs_extra_1.default.writeFile(targetIndexPath, indexSource + '\n'); | ||
echo(`Finished exporting ${matchedFiles.length} file(s).`); | ||
return matchedFiles; | ||
}); | ||
} | ||
exports.main = main; | ||
function echo(message) { | ||
@@ -92,12 +105,11 @@ console.log(message); | ||
function splitSourceExtsArg(sourceExtsArg) { | ||
return sourceExtsArg.split(',').map((ext) => ext.trim()); | ||
return sourceExtsArg.split(',').map((ext) => ext.trim().toLowerCase()); | ||
} | ||
function matchExts(exts, filePath) { | ||
return exts.some((ext) => filePath.endsWith(ext)); | ||
return exts.some((ext) => filePath.toLowerCase().endsWith(ext)); | ||
} | ||
function getPackageJson() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let config = yield fs_extra_1.default.readFile(path_1.default.join(__dirname, '../package.json'), 'utf-8'); | ||
return JSON.parse(config); | ||
return fs_extra_1.default.readJson(path_1.default.join(__dirname, '../package.json')); | ||
}); | ||
} |
{ | ||
"name": "index-tasukaru", | ||
"description": "Generate index file from CLI.", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"preferGlobal": true, | ||
@@ -47,3 +47,9 @@ "bin": { | ||
"LICENSE" | ||
], | ||
"keywords": [ | ||
"cli", | ||
"utility", | ||
"helper", | ||
"index" | ||
] | ||
} |
7347
115