Skip to content

Conversation

@divyanshsaraswat
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant