@haptiq/kit 0.4.0 ā 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/haptiq.kit.js +10 -2
- package/lib/ship.js +109 -3
- package/package.json +1 -1
package/bin/haptiq.kit.js
CHANGED
|
@@ -87,6 +87,14 @@ async function loadConfig(verbose = false) {
|
|
|
87
87
|
throw new Error('ship configuration must be an object');
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
if (config.ship.src !== undefined && (typeof config.ship.src !== 'string' || config.ship.src.trim() === '')) {
|
|
91
|
+
throw new Error('ship.src must be a non-empty string');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (config.ship.exclude !== undefined && !Array.isArray(config.ship.exclude)) {
|
|
95
|
+
throw new Error('ship.exclude must be an array');
|
|
96
|
+
}
|
|
97
|
+
|
|
90
98
|
if (config.ship.targets) {
|
|
91
99
|
if (typeof config.ship.targets !== 'object' || Array.isArray(config.ship.targets)) {
|
|
92
100
|
throw new Error('ship.targets must be an object');
|
|
@@ -134,8 +142,8 @@ async function loadConfig(verbose = false) {
|
|
|
134
142
|
*/
|
|
135
143
|
function createCommandHandler(taskName, taskFunction) {
|
|
136
144
|
return async (...args) => {
|
|
137
|
-
const options = args[args.length -
|
|
138
|
-
const positionalArgs = args.slice(0, -
|
|
145
|
+
const options = args[args.length - 2];
|
|
146
|
+
const positionalArgs = args.slice(0, -2);
|
|
139
147
|
try {
|
|
140
148
|
const config = await loadConfig(options.verbose);
|
|
141
149
|
await taskFunction(config, options, ...positionalArgs);
|
package/lib/ship.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { spawn } from 'child_process';
|
|
9
9
|
import { existsSync } from 'fs';
|
|
10
|
+
import readline from 'readline';
|
|
10
11
|
import path from 'path';
|
|
11
12
|
import { buildCSS } from './css.js';
|
|
12
13
|
import { buildJS } from './js.js';
|
|
@@ -49,7 +50,7 @@ async function ship(config, verbose, options = {}) {
|
|
|
49
50
|
|
|
50
51
|
for (const name of targetsToProcess) {
|
|
51
52
|
const targetConfig = targets[name] ?? {};
|
|
52
|
-
const dev = cliDev
|
|
53
|
+
const dev = (cliDev ?? targetConfig.dev) ?? false;
|
|
53
54
|
|
|
54
55
|
await buildCSS(config, verbose, dev);
|
|
55
56
|
await buildJS(config, verbose, { dev });
|
|
@@ -104,21 +105,123 @@ async function shipSingleTarget(name, targetConfig, shipConfig, verbose) {
|
|
|
104
105
|
|
|
105
106
|
const normalizedSrc = src.endsWith('/') ? src : `${src}/`;
|
|
106
107
|
|
|
108
|
+
const excludeArgs = exclude.flatMap(e => ['--exclude', e]);
|
|
109
|
+
const deleteArgs = deleteRemoved ? ['--delete', '--delete-excluded'] : [];
|
|
110
|
+
|
|
107
111
|
const args = [
|
|
108
112
|
'-az',
|
|
109
113
|
...(verbose ? ['-v'] : []),
|
|
110
|
-
...
|
|
111
|
-
...
|
|
114
|
+
...excludeArgs,
|
|
115
|
+
...deleteArgs,
|
|
112
116
|
normalizedSrc,
|
|
113
117
|
destination,
|
|
114
118
|
];
|
|
115
119
|
|
|
116
120
|
console.log(`š Shipping [${name}] ā ${destination}`);
|
|
117
121
|
|
|
122
|
+
if (deleteRemoved) {
|
|
123
|
+
const dryRunArgs = ['-azv', '-n', ...excludeArgs, ...deleteArgs, normalizedSrc, destination];
|
|
124
|
+
const deletions = await rsyncDryRun(dryRunArgs);
|
|
125
|
+
if (deletions.length > 0) {
|
|
126
|
+
await confirmDeletion(deletions, destination);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
118
130
|
await spawnRsync(args, verbose);
|
|
119
131
|
}
|
|
120
132
|
|
|
121
133
|
|
|
134
|
+
/**
|
|
135
|
+
* Run rsync in dry-run mode and return the list of files that would be deleted.
|
|
136
|
+
*
|
|
137
|
+
* @param {string[]} args - rsync arguments (must include -n and -v)
|
|
138
|
+
* @returns {Promise<string[]>}
|
|
139
|
+
*/
|
|
140
|
+
function rsyncDryRun(args) {
|
|
141
|
+
return new Promise((resolve, reject) => {
|
|
142
|
+
const childProcess = spawn('rsync', args, { shell: false });
|
|
143
|
+
|
|
144
|
+
let stdout = '';
|
|
145
|
+
let stderr = '';
|
|
146
|
+
let rejected = false;
|
|
147
|
+
|
|
148
|
+
childProcess.stdout.on('data', (data) => { stdout += data.toString(); });
|
|
149
|
+
childProcess.stderr.on('data', (data) => { stderr += data.toString(); });
|
|
150
|
+
|
|
151
|
+
childProcess.on('close', (code) => {
|
|
152
|
+
if (rejected) return;
|
|
153
|
+
if (code === 0) {
|
|
154
|
+
const deletions = stdout
|
|
155
|
+
.split('\n')
|
|
156
|
+
.filter(line => line.startsWith('deleting '))
|
|
157
|
+
.map(line => line.slice('deleting '.length).trim());
|
|
158
|
+
resolve(deletions);
|
|
159
|
+
} else {
|
|
160
|
+
reject(new Error(`rsync dry-run failed: ${stderr.trim()}`));
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
childProcess.on('error', (err) => {
|
|
165
|
+
rejected = true;
|
|
166
|
+
if (err.code === 'ENOENT') {
|
|
167
|
+
reject(new Error('rsync not found. Please ensure rsync is installed on your system.'));
|
|
168
|
+
} else {
|
|
169
|
+
reject(new Error(`Failed to start rsync: ${err.message}`));
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Show a deletion preview and require the user to type DELETE to continue.
|
|
178
|
+
*
|
|
179
|
+
* @param {string[]} deletions - Files that would be deleted
|
|
180
|
+
* @param {string} destination - Rsync destination (shown in the warning)
|
|
181
|
+
* @returns {Promise<void>}
|
|
182
|
+
*/
|
|
183
|
+
async function confirmDeletion(deletions, destination) {
|
|
184
|
+
const count = deletions.length;
|
|
185
|
+
const shown = deletions.slice(0, 7);
|
|
186
|
+
|
|
187
|
+
console.log(`\nā ļø ${count} ${count === 1 ? 'file' : 'files'} will be permanently deleted from:`);
|
|
188
|
+
console.log(` ${destination}\n`);
|
|
189
|
+
|
|
190
|
+
for (const file of shown) {
|
|
191
|
+
console.log(` - ${file}`);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (count > 7) {
|
|
195
|
+
console.log(` ⦠and ${count - 7} more files.`);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
console.log('\nWARNING: These files will be removed irrevocably!\n');
|
|
199
|
+
console.log('To proceed, type DELETE and press Enter (Ctrl+C to abort):');
|
|
200
|
+
|
|
201
|
+
const answer = await readLine();
|
|
202
|
+
|
|
203
|
+
if (answer !== 'DELETE') {
|
|
204
|
+
throw new Error('Aborted ā sync cancelled.');
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Read a single line from stdin.
|
|
211
|
+
*
|
|
212
|
+
* @returns {Promise<string>}
|
|
213
|
+
*/
|
|
214
|
+
function readLine() {
|
|
215
|
+
return new Promise((resolve) => {
|
|
216
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
217
|
+
rl.question('', (answer) => {
|
|
218
|
+
rl.close();
|
|
219
|
+
resolve(answer);
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
|
|
122
225
|
/**
|
|
123
226
|
* Spawn rsync as a child process without shell interpolation
|
|
124
227
|
*
|
|
@@ -131,6 +234,7 @@ function spawnRsync(args, verbose) {
|
|
|
131
234
|
const childProcess = spawn('rsync', args, { shell: false });
|
|
132
235
|
|
|
133
236
|
let stderr = '';
|
|
237
|
+
let rejected = false;
|
|
134
238
|
|
|
135
239
|
if (verbose) {
|
|
136
240
|
childProcess.stdout.on('data', (data) => process.stdout.write(data));
|
|
@@ -141,6 +245,7 @@ function spawnRsync(args, verbose) {
|
|
|
141
245
|
});
|
|
142
246
|
|
|
143
247
|
childProcess.on('close', (code) => {
|
|
248
|
+
if (rejected) return;
|
|
144
249
|
if (code === 0) {
|
|
145
250
|
resolve();
|
|
146
251
|
} else {
|
|
@@ -149,6 +254,7 @@ function spawnRsync(args, verbose) {
|
|
|
149
254
|
});
|
|
150
255
|
|
|
151
256
|
childProcess.on('error', (err) => {
|
|
257
|
+
rejected = true;
|
|
152
258
|
if (err.code === 'ENOENT') {
|
|
153
259
|
reject(new Error('rsync not found. Please ensure rsync is installed on your system.'));
|
|
154
260
|
} else {
|