Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const assertDirectory = (t, directory, mode = 0o777 & (~process.umask())) => {
// Setting `mode` on `fs.mkdir` on Windows doesn't seem to work
if (process.platform === 'win32') {
mode = 0o666;
}
t.true(pathType.isDirectorySync(directory));
t.is(fs.statSync(directory).mode & 0o777, mode);
};
if (cacheValue > 0) {
streamOptions.maxAge = flags.cache;
} else if (cacheValue === 0) {
// Disable the cache control by `send`, as there's no support for `no-cache`.
// Set header manually.
streamOptions.cacheControl = false;
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
} else if (flags.single) {
// Cache assets of single page applications for a day.
// Later in the code, we'll define that `index.html` never
// gets cached!
streamOptions.maxAge = 86400000;
}
// Check if directory
if (relatedExists && (yield pathType.dir(related))) {
// Normalize path to trailing slash
// Otherwise problems like #70 will occur
const url = parse(req.url);
if (url.pathname.substr(-1) !== '/') {
url.pathname += '/';
const newPath = format(url);
res.writeHead(302, {
Location: newPath
});
res.end();
return;
}
} else if (!relatedExists) { // If not exists return an error 404
return httpRequest.micro({
req,
res,
code: 404
});
}
const streamOptions = {};
if (flags.cache) {
streamOptions.maxAge = flags.cache;
}
// Check if directory
if (pathType.isDirectorySync(related)) {
const url = parse(req.url);
// If the url doesn't end with a slash, add it and redirect the page
if (url.pathname.substr(-1) !== '/') {
url.pathname += '/';
const newPath = format(url);
httpRequest.logRequests({
req,
res,
code: 301
}, 301);
res.writeHead(301, {
Location: newPath
});
module.exports = (fp, opts) => {
if (typeof fp !== 'string') {
opts = fp;
fp = '.';
}
opts = opts || {};
return pathType.dir(fp)
.then(isDir => {
if (isDir) {
fp = path.join(fp, 'package.json');
}
return loadJsonFile(fp);
})
.then(x => {
if (opts.normalize !== false) {
require('normalize-package-data')(x);
}
return x;
});
};
module.exports.sync = (fp, opts) => {
if (typeof fp !== 'string') {
opts = fp;
fp = '.';
}
opts = opts || {};
fp = pathType.dirSync(fp) ? path.join(fp, 'package.json') : fp;
const x = loadJsonFile.sync(fp);
if (opts.normalize !== false) {
require('normalize-package-data')(x);
}
return x;
};
) {
const details = server.address();
const {isTTY} = process.stdout;
const shutdown = () => {
server.close();
process.exit(0);
};
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
let isDir;
try {
isDir = yield pathType.dir(current);
} catch (err) {
isDir = false;
}
if (!isDir) {
const base = basename(current);
console.error(
chalk.red(`Specified directory ${chalk.bold(`"${base}"`)} doesn't exist!`)
);
process.exit(1);
}
if (process.env.NODE_ENV !== 'production') {
let message = chalk.green('Serving!');
server.close();
global.utils.logger.log(chalk.red(pkg.name + ' stopped.'));
process.exit(0);
};
process.on('SIGINT', () => {
stopServer();
});
process.on('kill', () => {
stopServer();
});
let isDir;
try {
isDir = pathType.isDirectorySync(current);
} catch (err) {
isDir = false;
}
if (!isDir) {
const base = basename(current);
console.error(
chalk.red(`Specified directory ${chalk.bold(`"${base}"`)} doesn't exist!`)
);
process.exit(1);
}
let message = '\n' + chalk.blue(pkg.title + ' is Online!') + ' 🚀';
let notificationTxt = 'The server is running.';
}
try {
files = yield fs.readdir(dir);
} catch (err) {
throw err;
}
for (const file of files) {
const filePath = path.resolve(dir, file);
const index = files.indexOf(file);
const details = path.parse(filePath);
details.relative = path.join(subPath, details.base);
if (yield pathType.dir(filePath)) {
details.base += '/';
} else {
details.ext = details.ext.split('.')[1] || 'txt';
let fileStats;
try {
fileStats = yield fs.stat(filePath);
} catch (err) {
throw err;
}
details.size = filesize(fileStats.size, {round: 0});
}
details.title = details.base;
const assertIgnored = (filepath) => {
const failMessage = `File must be ignored by git. Fix: echo '${path.basename(filepath)}' >> .gitignore`;
let ignores;
try {
ignores = fs.readFileSync(path.join(filepath, '..', '.gitignore'), 'utf8');
}
catch (error) {
if (error.code === 'ENOENT') {
if (!pathType.dirSync(path.join(filepath, '..', '.git'))) {
return;
}
throw new Error(failMessage);
}
throw error;
}
if (!ignores.split(/\r?\n/u).includes(path.basename(filepath))) {
throw new Error(failMessage);
}
};
if (!fs.existsSync(dir)) {
return false;
}
filesRaw = yield fs.readdir(dir);
let files = [];
for (const file of filesRaw) {
const filePath = path.resolve(dir, file);
const details = path.parse(filePath);
details.title = details.base;
details.stats = {};
if (pathType.isDirectorySync(filePath)) {
details.base += '/';
details.type = 'dir';
details.stats.size = '—';
} else {
details.ext = details.ext.split('.')[1] || '';
details.type = 'file';
}
details.relative = path.join(subPath, details.base);
if (fu.isNotIgnoredPath(details.relative)) {
const fileStats = yield fs.stat(filePath);
if (!_.isString(details.stats.size)) {
details.stats.sizeRaw = fileStats.size;
details.stats.size = filesize(fileStats.size, {