Repository navigation
Expand file tree
/
Copy pathpackage-source.js
More file actions
448 lines (369 loc) · 11.2 KB
/
Copy pathpackage-source.js
File metadata and controls
448 lines (369 loc) · 11.2 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/env node
// Copyright (c) Meta Platforms, Inc. and affiliates.
/**
* @file package-source.js
* @description Packages XDS raw source files into a distributable tarball
*
* Usage: node scripts/package-source.js
* Output: dist/xds-core-{version}.tgz
*/
const fs = require('node:fs');
const path = require('node:path');
const { execSync } = require('node:child_process');
const ROOT = path.resolve(__dirname, '..');
const CORE_DIR = path.join(ROOT, 'packages', 'core');
const SRC_DIR = path.join(CORE_DIR, 'src');
const DIST_DIR = path.join(ROOT, 'dist-source');
const AGENT_TOOLS_DIR = path.join(ROOT, 'packages', 'cli');
const PKG = require(path.join(CORE_DIR, 'package.json'));
// Files/patterns to exclude from distribution
const EXCLUDE_PATTERNS = [
/\.test\.tsx?$/, // Test files
/\.spec\.tsx?$/, // Spec files
/__tests__/, // Test directories
/__mocks__/, // Mock directories
];
function shouldInclude(filePath) {
return !EXCLUDE_PATTERNS.some(pattern => pattern.test(filePath));
}
function copyDir(src, dest) {
fs.mkdirSync(dest, { recursive: true });
const entries = fs.readdirSync(src, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
const relativePath = path.relative(SRC_DIR, srcPath);
if (!shouldInclude(relativePath)) {
console.log(` Excluding: ${relativePath}`);
continue;
}
if (entry.isDirectory()) {
copyDir(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
console.log(` Copied: ${relativePath}`);
}
}
}
/**
* Derive source exports from the "source" condition in package.json exports.
*
* The canonical exports map is maintained by scripts/sync-exports.js, which
* adds a "source" condition pointing to the TypeScript source for each entry.
* This function reads those "source" paths and strips the "src/" prefix
* (since the source distribution root IS the src/ directory).
*
* Falls back to auto-discovery if "source" conditions are missing.
*/
function deriveSourceExports() {
const sourceExports = {};
for (const [key, value] of Object.entries(PKG.exports)) {
// Handle string exports (e.g., "./reset.css": "./src/reset.css")
if (typeof value === 'string') {
if (value.startsWith('./src/')) {
sourceExports[key] = value.replace('./src/', './');
} else if (!value.includes('/dist/')) {
sourceExports[key] = value;
}
continue;
}
// Handle object exports — prefer the "source" condition
if (typeof value === 'object' && value !== null) {
if (value.source) {
// Strip ./src/ prefix since the tarball root is src/
sourceExports[key] = value.source.replace('./src/', './');
} else {
// Fallback: derive from import/require path
const importPath = value.import || value.require || value.types;
if (!importPath) continue;
const sourcePath = importPath
.replace('./dist/', './')
.replace(/\.mjs$/, '.ts')
.replace(/\.js$/, '.ts');
sourceExports[key] = sourcePath;
}
}
}
// Auto-discover component directories with index.ts that aren't in exports
const srcEntries = fs.readdirSync(SRC_DIR, { withFileTypes: true });
for (const entry of srcEntries) {
if (!entry.isDirectory()) continue;
const exportKey = './' + entry.name;
if (sourceExports[exportKey]) continue;
const indexPath = path.join(SRC_DIR, entry.name, 'index.ts');
if (fs.existsSync(indexPath)) {
sourceExports[exportKey] = './' + entry.name + '/index.ts';
}
}
return sourceExports;
}
function createPackageJson() {
// Create a package.json for source consumption
const sourcePackage = {
name: '@astryxdesign/core',
version: PKG.version,
description: 'XDS Design System - Raw Source Distribution',
// Point directly to source
main: './index.ts',
module: './index.ts',
types: './index.ts',
sideEffects: [
'**/*.stylex.ts',
'**/*.css',
],
// Derive exports from core package.json
exports: deriveSourceExports(),
peerDependencies: PKG.peerDependencies,
dependencies: PKG.dependencies,
// Consumer build requirements
engines: {
node: '>=18.0.0',
},
// Metadata
repository: PKG.repository,
license: PKG.license || 'MIT',
// Instructions for consumers
xds: {
distribution: 'source',
requirements: [
'TypeScript >= 5.0',
'@stylexjs/babel-plugin',
'Babel or compatible build tool',
],
},
};
return sourcePackage;
}
function createReadme() {
return `# XDS Core - Raw Source Distribution
This package contains the raw TypeScript source files for XDS components.
## Quick Start
1. Extract to your project:
\`\`\`bash
mkdir -p src/xds
tar -xzf xds-core-${PKG.version}.tgz -C src/xds
\`\`\`
2. Run setup (optional - configures AI coding assistant docs):
\`\`\`bash
node src/xds/setup.mjs
\`\`\`
3. Import components:
\`\`\`tsx
import { XDSButton } from './xds/Button';
import { XDSTheme, defaultTheme } from './xds/theme';
\`\`\`
## Requirements
- **TypeScript** >= 5.0
- **StyleX Babel Plugin** - \`@stylexjs/babel-plugin\`
- **React** >= 19.0.0
## StyleX Configuration
### Next.js (Babel + PostCSS)
\`\`\`js
// babel.config.js
module.exports = {
presets: ['next/babel'],
plugins: [
['@stylexjs/babel-plugin', {
dev: process.env.NODE_ENV !== 'production',
stylexSheetName: '<>',
genConditionalClasses: true,
}],
],
};
\`\`\`
\`\`\`js
// postcss.config.mjs
import stylexPlugin from '@stylexjs/postcss-plugin';
export default {
plugins: [
stylexPlugin({
include: [
'src/**/*.{js,ts,tsx}',
'src/xds/**/*.{ts,tsx}', // Include XDS source
],
}),
],
};
\`\`\`
### Vite (unplugin)
\`\`\`js
// vite.config.ts
import { stylexPlugin } from '@stylexjs/unplugin/vite';
export default {
plugins: [
stylexPlugin({
stylexImports: ['@stylexjs/stylex'],
include: [
/\\.tsx?$/,
/xds\\/.*\\.tsx?$/, // Include XDS source
],
}),
],
};
\`\`\`
## Theming
Wrap your app with the theme provider:
\`\`\`tsx
import { XDSTheme, defaultTheme } from './xds/theme';
function App() {
return (
<XDSTheme theme={defaultTheme}>
{/* Your app */}
</XDSTheme>
);
}
\`\`\`
## AI Coding Assistant Support
XDS includes documentation optimized for AI coding assistants (Claude, Cursor, etc.).
Run the setup script to install:
\`\`\`bash
node src/xds/setup.mjs
\`\`\`
This will:
- Ask if you want to add XDS guidance to your AGENTS.md
- Create a \`.astryx-docs/\` folder with component documentation
## Version
${PKG.version}
`;
}
function createSetupScript() {
return `#!/usr/bin/env node
/**
* XDS Setup Script
* Configures AI coding assistant documentation
*/
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as readline from 'node:readline';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
function ask(question) {
return new Promise((resolve) => {
rl.question(question, (answer) => {
resolve(answer.toLowerCase().trim());
});
});
}
async function main() {
console.log('\\n🎨 XDS Setup\\n');
const answer = await ask('Add XDS documentation to AGENTS.md for AI coding assistants? (y/N) ');
if (answer === 'y' || answer === 'yes') {
// Find project root (look for package.json)
let projectRoot = process.cwd();
for (let i = 0; i < 10; i++) {
if (fs.existsSync(path.join(projectRoot, 'package.json'))) {
break;
}
const parent = path.dirname(projectRoot);
if (parent === projectRoot) break;
projectRoot = parent;
}
// Run agents-md script
const agentsMdPath = path.join(__dirname, 'cli', 'bin', 'astryx.mjs');
if (fs.existsSync(agentsMdPath)) {
const { execSync } = await import('node:child_process');
try {
execSync(\`node "\${agentsMdPath}" agent-docs\`, {
cwd: projectRoot,
stdio: 'inherit'
});
} catch (e) {
console.error('Failed to install AGENTS.md:', e.message);
}
} else {
console.log('⚠️ CLI not found at:', agentsMdPath);
}
} else {
console.log('\\nSkipped AGENTS.md setup.');
console.log('You can run this later with: node src/xds/setup.mjs');
}
console.log('\\n✅ XDS setup complete!\\n');
rl.close();
}
main();
`;
}
function copyAgentTools(dest) {
const agentToolsDest = path.join(dest, 'cli');
if (!fs.existsSync(AGENT_TOOLS_DIR)) {
console.log(' Warning: cli package not found, skipping');
return;
}
fs.mkdirSync(agentToolsDest, { recursive: true });
// Copy bin directory
const binSrc = path.join(AGENT_TOOLS_DIR, 'clients', 'cli', 'bin');
const binDest = path.join(agentToolsDest, 'bin');
if (fs.existsSync(binSrc)) {
fs.mkdirSync(binDest, { recursive: true });
for (const file of fs.readdirSync(binSrc)) {
fs.copyFileSync(path.join(binSrc, file), path.join(binDest, file));
console.log(` Copied: cli/bin/${file}`);
}
}
// Copy docs directory
const docsSrc = path.join(AGENT_TOOLS_DIR, 'assets', 'docs');
const docsDest = path.join(agentToolsDest, 'docs');
if (fs.existsSync(docsSrc)) {
fs.mkdirSync(docsDest, { recursive: true });
for (const file of fs.readdirSync(docsSrc)) {
fs.copyFileSync(path.join(docsSrc, file), path.join(docsDest, file));
console.log(` Copied: cli/docs/${file}`);
}
}
}
function main() {
console.log(`\nPackaging XDS Core v${PKG.version} (source distribution)\n`);
// Clean dist directory
if (fs.existsSync(DIST_DIR)) {
fs.rmSync(DIST_DIR, { recursive: true });
}
fs.mkdirSync(DIST_DIR, { recursive: true });
// Copy source files
console.log('Copying source files...');
copyDir(SRC_DIR, DIST_DIR);
// Create package.json
console.log('\nCreating package.json...');
const packageJson = createPackageJson();
fs.writeFileSync(
path.join(DIST_DIR, 'package.json'),
JSON.stringify(packageJson, null, 2)
);
// Create README
console.log('Creating README.md...');
fs.writeFileSync(
path.join(DIST_DIR, 'README.md'),
createReadme()
);
// Create setup script
console.log('Creating setup.mjs...');
fs.writeFileSync(
path.join(DIST_DIR, 'setup.mjs'),
createSetupScript()
);
// Copy CLI tools
console.log('\nCopying CLI tools...');
copyAgentTools(DIST_DIR);
// Create tarball
console.log('\nCreating tarball...');
const tarballName = `xds-core-${PKG.version}.tgz`;
execSync(`tar -czf ${tarballName} -C dist-source .`, { cwd: ROOT });
// Move to dist directory (create if needed)
const distPath = path.join(ROOT, 'dist');
if (!fs.existsSync(distPath)) {
fs.mkdirSync(distPath);
}
fs.renameSync(
path.join(ROOT, tarballName),
path.join(distPath, tarballName)
);
// Clean up intermediate directory
fs.rmSync(DIST_DIR, { recursive: true });
console.log(`\nDone! Package created at: dist/${tarballName}`);
console.log(`\nTo install in a project:`);
console.log(` tar -xzf ${tarballName} -C ./src/xds`);
}
main();