@haptiq/kit 0.4.0 → 0.5.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.
Files changed (2) hide show
  1. package/lib/ship.js +102 -2
  2. package/package.json +1 -1
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';
@@ -104,21 +105,120 @@ 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
- ...exclude.flatMap(e => ['--exclude', e]),
111
- ...(deleteRemoved ? ['--delete', '--delete-excluded'] : []),
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
+
147
+ childProcess.stdout.on('data', (data) => { stdout += data.toString(); });
148
+ childProcess.stderr.on('data', (data) => { stderr += data.toString(); });
149
+
150
+ childProcess.on('close', (code) => {
151
+ if (code === 0) {
152
+ const deletions = stdout
153
+ .split('\n')
154
+ .filter(line => line.startsWith('deleting '))
155
+ .map(line => line.slice('deleting '.length).trim());
156
+ resolve(deletions);
157
+ } else {
158
+ reject(new Error(`rsync dry-run failed: ${stderr.trim()}`));
159
+ }
160
+ });
161
+
162
+ childProcess.on('error', (err) => {
163
+ if (err.code === 'ENOENT') {
164
+ reject(new Error('rsync not found. Please ensure rsync is installed on your system.'));
165
+ } else {
166
+ reject(new Error(`Failed to start rsync: ${err.message}`));
167
+ }
168
+ });
169
+ });
170
+ }
171
+
172
+
173
+ /**
174
+ * Show a deletion preview and require the user to type DELETE to continue.
175
+ *
176
+ * @param {string[]} deletions - Files that would be deleted
177
+ * @param {string} destination - Rsync destination (shown in the warning)
178
+ * @returns {Promise<void>}
179
+ */
180
+ async function confirmDeletion(deletions, destination) {
181
+ const count = deletions.length;
182
+ const shown = deletions.slice(0, 7);
183
+
184
+ console.log(`\nāš ļø ${count} ${count === 1 ? 'file' : 'files'} will be permanently deleted from:`);
185
+ console.log(` ${destination}\n`);
186
+
187
+ for (const file of shown) {
188
+ console.log(` - ${file}`);
189
+ }
190
+
191
+ if (count > 7) {
192
+ console.log(` … and ${count - 7} more files.`);
193
+ }
194
+
195
+ console.log('\nWARNING: These files will be removed irrevocably!\n');
196
+ console.log('To proceed, type DELETE and press Enter (Ctrl+C to abort):');
197
+
198
+ const answer = await readLine();
199
+
200
+ if (answer !== 'DELETE') {
201
+ throw new Error('Aborted — sync cancelled.');
202
+ }
203
+ }
204
+
205
+
206
+ /**
207
+ * Read a single line from stdin.
208
+ *
209
+ * @returns {Promise<string>}
210
+ */
211
+ function readLine() {
212
+ return new Promise((resolve) => {
213
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
214
+ rl.question('', (answer) => {
215
+ rl.close();
216
+ resolve(answer);
217
+ });
218
+ });
219
+ }
220
+
221
+
122
222
  /**
123
223
  * Spawn rsync as a child process without shell interpolation
124
224
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haptiq/kit",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Build tools for Haptiq projects.",
5
5
  "keywords": [
6
6
  "build",