-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·138 lines (117 loc) · 4.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const path = require('path');
const fs = require('fs-extra');
const SSI_EXTENSIONS = [
'.html',
'.shtml',
];
const SSI_REGEX = new RegExp(/(.*)<!--#include\s+virtual="([\d\w\.\/\$\-\_]+)"\s?-->(.*)/, 'sm');
const log = (...args) => {
console.log(...args);
}
const error = (...args) => {
console.log(...args);
}
const progress = (...args) => {
console.log('=>', ...args);
}
const checkpoint = (...args) => {
console.log(' ✓', ...args);
}
const read_file = path => {
return fs.readFileSync(path, {
encoding: 'utf8',
});
}
const dessi = async () => {
// process arguments
const args = process.argv.slice(1);
const cwd = process.cwd();
let root_dir,
target_dir,
source_dir;
for (const arg of args) {
if (arg.startsWith('--root=')) {
root_dir = path.join(cwd, arg.substr(7));
} else if (arg.startsWith('--target=')) {
target_dir = path.join(cwd, arg.substr(9));
} else if (arg.startsWith('--source=')){
source_dir = path.join(cwd, arg.substr(9));
}
}
log(`Expanding from:\n\t${source_dir}`);
log(`Saving to:\n\t${target_dir}`);
log(`Path root:\n\t${root_dir}`);
log('');
if (root_dir === undefined) {
root_dir = source_dir;
}
if (!(
source_dir && target_dir && root_dir
)) {
error('Please specify the --source and --target directories.');
return;
}
// copy source_dir to target_dir, parsing any relevant files
const ssi_expand = (content) => {
const translate_path = (root_dir, rest) => path.join(root_dir, rest);
const parts = content.split('<!--#')
.map((bit, idx) => idx ? '<!--#' + bit : bit);
const result = [];
for (const part of parts) {
const match = SSI_REGEX.exec(part);
if (match !== null) {
const [_discard, pre, includePath, post] = match;
let file_contents = read_file(translate_path(root_dir, includePath));
if (SSI_EXTENSIONS.includes(path.extname(includePath))) {
log(`Following nested include ${includePath}`);
file_contents = ssi_expand(file_contents);
}
result.push(pre);
result.push(file_contents);
result.push(post);
} else {
result.push(part);
}
}
return result.join('');
}
const process_dir = async (working_source_dir, working_target_dir) => {
const children = await fs.readdir(working_source_dir);
for (const child of children) {
const is_dir = fs.lstatSync(path.join(working_source_dir, child)).isDirectory();
const absolute_source_path = path.join(working_source_dir, child);
const absolute_target_path = path.join(working_target_dir, child);
if (path.basename(absolute_source_path).startsWith('.')) {
checkpoint(`Ignoring dotfile ${path.relative(cwd, absolute_source_path)}`);
continue;
} else {
progress(`Processing ${path.relative(cwd, absolute_source_path)}`);
}
if (is_dir) {
if (path.join(absolute_source_path, path.sep).includes(target_dir)) {
checkpoint(`Ignoring nested target dir ${path.relative(cwd, absolute_source_path)}`);
} else {
await fs.ensureDir(absolute_target_path);
await process_dir(
absolute_source_path,
absolute_target_path
);
}
} else {
if (SSI_EXTENSIONS.includes(path.extname(absolute_source_path))) {
fs.writeFileSync(
absolute_target_path,
ssi_expand(read_file(absolute_source_path)),
{
encoding: 'utf8',
}
);
} else {
await fs.copy(absolute_source_path, absolute_target_path);
}
}
}
}
process_dir(source_dir, target_dir);
}
module.exports = dessi;