A2UI icon indicating copy to clipboard operation
A2UI copied to clipboard

fix(lit): make copy-spec script cross-platform compatible

Open divyanshsaraswat opened this issue 4 days ago • 0 comments

Objective: Resolve build failures on Windows operating systems caused by Unix-specific shell commands in the copy-spec script.

1. The Problem

The original package.json script for copy-spec relied on shell commands that are native to Linux and macOS but incompatible with the standard Windows Command Prompt (cmd.exe):

  • mkdir -p: The -p flag (create parent directories) is not supported by the Windows mkdir command.
  • cp: This is a Unix copy command; Windows uses copy or xcopy.

Original Code (Fails on Windows):

"copy-spec": {
  "command": "mkdir -p src/0.8/schemas && cp ../../specification/0.8/json/*.json src/0.8/schemas",
  ...
}

2. The Solution

We replaced the platform-dependent shell commands with a Node.js script. Since Node.js is already a requirement for the project, using its built-in fs (File System) module allows us to perform file operations that work identically on Windows, Linux, and macOS.

3. Implementation Details

A. Created scripts/copy-spec.js

We added a new script that handles the logic programmatically:

  1. Resolve Paths: Uses path.resolve to correctly locate the source (../../specification/0.8/json) and destination (src/0.8/schemas) directories relative to the script location, ensuring path separators (/ vs \) are handled correctly for the OS.
  2. Ensure Directory Exists: Uses fs.mkdirSync(destDir, { recursive: true }). The recursive: true option is the cross-platform equivalent of mkdir -p.
  3. Copy Files: properly iterates through files in the source directory and uses fs.copyFileSync to copy them one by one.

B. Updated package.json

We modified the copy-spec entry in package.json to execute this new script using the node runtime.

New Code (Works Everywhere):

"copy-spec": {
  "command": "node scripts/copy-spec.js",
  "files": [
    "../../specification/0.8/json/*.json",
    "scripts/copy-spec.js" 
  ],
  ...
}

4. Key Benefits

  • Cross-Platform Compatibility: The project can now be built by contributors using Windows without needing WSL (Windows Subsystem for Linux) or Git Bash.
  • Stability: Eliminates "syntax error" crashes during the build process on Windows.
  • Maintainability: JavaScript logic is easier to read, debug, and extend than maintaining complex, chained shell commands.

divyanshsaraswat avatar Dec 26 '25 07:12 divyanshsaraswat