const fs = require('fs');
const path = require('path');
// Setup variables
const docroot = 'wtest';
const topdirs = [
'src',
'dist',
'misc',
];
const srcdirs = [
'css',
'fonts',
'js',
];
const index = 'index.html';
const title = 'W TEST';
const mainjs = 'main.js';
const cssfiles = [
'01-fonts.css',
'02-normalize.css',
'03-borderbox.css',
'02-normalize.css',
'dev-styles.css',
];
// Create an associative map from cssfiles array
const fileMap = new Map();
cssfiles.forEach(file => {
let key = '';
if (file.includes('fonts')) {
key = 'fonts';
} else if (file.includes('normalize')) {
key = 'normalize';
} else if (file.includes('borderbox')) {
key = 'borderbox';
} else if (file.includes('colors')) {
key = 'colors';
} else if (file.includes('dev-styles')) {
key = 'dev_styles';
}
if (key) {
fileMap.set(key, file);
}
});
// Create document root directory
if (!fs.existsSync(docroot)) {
fs.mkdirSync(docroot, { recursive: true });
}
// Create top level directories
topdirs.forEach(item => {
fs.mkdirSync(path.join(docroot, item), { recursive: true });
});
// Create /docroot/src subdirectories
srcdirs.forEach(item => {
fs.mkdirSync(path.join(docroot, 'src', item), { recursive: true });
});
// Create index.html file
fs.closeSync(fs.openSync(path.join(docroot, 'src', index), 'w'));
// Create /docroot/src/main.js file
fs.closeSync(fs.openSync(path.join(docroot, 'src', 'js', mainjs), 'w'));
// Create CSS files on /docroot/src/css
cssfiles.forEach(item => {
fs.closeSync(fs.openSync(path.join(docroot, 'src', 'css', item), 'w'));
});
// Write /docroot/index.html base markup
const html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width initial-scale=1.0">
<title>${title}</title>
<link rel="stylesheet" href="/css/${fileMap.get('dev_styles')}">
</head>
<body>
<header>
</header>
<main>
</main>
<footer>
</footer>
<script src="/js/${mainjs}"></script>
</body>
</html>
`;
fs.writeFileSync(path.join(docroot, 'src', index), html);
// Write to 02-borderbox.css file
const borderbox = `html {
box-sizing: border-box;
}
*,
*::before,
*::after {
box-sizing: inherit;
}
`;
if (fileMap.has('borderbox')) {
fs.writeFileSync(path.join(docroot, 'src', 'css', fileMap.get('borderbox')), borderbox);
}
// Write to 03-colors.css file
const csscolor = `:root {
}
`;
if (fileMap.has('colors')) {
fs.writeFileSync(path.join(docroot, 'src', 'css', fileMap.get('colors')), csscolor);
}
// Write to dev-styles.css file
let css_imports = '';
cssfiles.forEach(file => {
css_imports += `@import url(${file});\n`;
});
// Remove the trailing newline
css_imports = css_imports.trimEnd();
if (fileMap.has('dev_styles')) {
fs.writeFileSync(path.join(docroot, 'src', 'css', fileMap.get('dev_styles')), css_imports);
}
// Write to .gitignore file
const gitignore = `/dist/
/misc/
`;
fs.writeFileSync(path.join(docroot, '.gitignore'), gitignore);
Please note that this script includes equivalent operations for creating directories, files, and writing content to files. It also handles creating a simple map for associating CSS files with certain keys, similar to the associative array in the Bash script.
To run this JavaScript/Node.js code, save it to a .js file, for example script.js, and execute it in your terminal with node script.js. Make sure your Node.js environment is properly set up and that you have permissions to create directories and files in the location where you are executing the script.