Skip to content
This repository has been archived by the owner on Nov 26, 2021. It is now read-only.

Commit

Permalink
Add postinstall scripts which throw an error if the os or cpu is diff…
Browse files Browse the repository at this point in the history
…erent to what the package supports

This is a required for npm ci to work. npm/cli#558
  • Loading branch information
JakeChampion committed Mar 11, 2020
1 parent 2d3e3b9 commit 79d8224
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 624 deletions.
567 changes: 3 additions & 564 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Expand Up @@ -6,11 +6,11 @@
"repository": "Financial-times/sass",
"bin": "sass.bat",
"optionalDependencies": {
"@financial-times/sass-linux-ia32": "*",
"@financial-times/sass-linux-x64": "*",
"@financial-times/sass-macos-x64": "*",
"@financial-times/sass-windows-ia32": "*",
"@financial-times/sass-windows-x64": "*"
"@financial-times/sass-linux-ia32": "file:./sass-linux-ia32",
"@financial-times/sass-linux-x64": "file:./sass-linux-x64",
"@financial-times/sass-macos-x64": "file:./sass-macos-x64",
"@financial-times/sass-windows-ia32": "file:./sass-windows-ia32",
"@financial-times/sass-windows-x64": "file:./sass-windows-x64"
},
"scripts": {
"postinstall": "node ./postinstall.js",
Expand Down
104 changes: 54 additions & 50 deletions postinstall.js
Expand Up @@ -4,65 +4,69 @@ const fs = require("fs");
const path = require("path");

let platformSpecificPackagePath;
if (process.platform === "win32" && process.arch === "x64") {
platformSpecificPackagePath = require.resolve("@financial-times/sass-windows-x64");
} else if (process.platform === "win32" && process.arch === "ia32") {
platformSpecificPackagePath = require.resolve("@financial-times/sass-windows-ia32");
} else if (process.platform === "darwin") {
platformSpecificPackagePath = require.resolve("@financial-times/sass-macos-x64");
} else if (process.platform === "linux" && process.arch === "x64") {
platformSpecificPackagePath = require.resolve("@financial-times/sass-linux-x64");
} else if (process.platform === "linux" && process.arch === "ia32") {
platformSpecificPackagePath = require.resolve("@financial-times/sass-linux-ia32");
const windows64bit = process.platform === "win32" && process.arch === "x64";
const windows32bit = process.platform === "win32" && process.arch === "ia32";
const mac = process.platform === "darwin";
const linux64bit = process.platform === "linux" && process.arch === "x64";
const linux32bit = process.platform === "linux" && process.arch === "ia32";

if (windows64bit) {
platformSpecificPackagePath = require.resolve(
"@financial-times/sass-windows-x64"
);
} else if (windows32bit) {
platformSpecificPackagePath = require.resolve(
"@financial-times/sass-windows-ia32"
);
} else if (mac) {
platformSpecificPackagePath = require.resolve(
"@financial-times/sass-macos-x64"
);
} else if (linux64bit) {
platformSpecificPackagePath = require.resolve(
"@financial-times/sass-linux-x64"
);
} else if (linux32bit) {
platformSpecificPackagePath = require.resolve(
"@financial-times/sass-linux-ia32"
);
} else {
throw new Error(
"@financial-times/sass does not have a precompiled binary for the platform/architecture you are using. Please contact Origami or open an issue on https://github.com/Financial-Times/sass/issues"
);
}

const platformSpecificSassPath = path.join(path.dirname(platformSpecificPackagePath), 'dart-sass')

// We need to remove the sass entrypoint file if it exists already.
if (fs.existsSync(path.join(__dirname, './sass.bat'))) {
fs.unlinkSync(path.join(__dirname, './sass.bat'));
}
const platformSpecificSassPath = path.join(
path.dirname(platformSpecificPackagePath),
"dart-sass"
);

// Create our own sass entrypoint which uses absolute paths to the dartvm and snapshot.
// This will ensure it works when symlinked to, which the entrypoint file in dart-sass
// does not support on macos.
if (process.platform === "win32") {
fs.writeFileSync(
path.join(__dirname, "sass.bat"),
`
@echo off
REM This script drives the standalone dart-sass package, which bundles together a
REM Dart executable and a snapshot of dart-sass.
set SCRIPTPATH=%~dp0
set arguments=%*
"${path.join(platformSpecificSassPath, "/src/dart.exe")}" "${path.join(
platformSpecificSassPath,
"/src/sass.dart.snapshot"
)}" %arguments%
`,
{
encoding: "utf-8",
mode: 0o777
}
);
} else {
fs.writeFileSync(
path.join(__dirname, "sass.bat"),
`
const windowsEntrypoint = `
@echo off
REM This script drives the standalone dart-sass package, which bundles together a
REM Dart executable and a snapshot of dart-sass.
set SCRIPTPATH=%~dp0
set arguments=%*
"${path.join(platformSpecificSassPath, "/src/dart.exe")}" "${path.join(
platformSpecificSassPath,
"/src/sass.dart.snapshot"
)}" %arguments%`;

const linuxAndMacEntrypoint = `
#!/bin/sh
exec "${path.join(platformSpecificSassPath, "/src/dart")}" "${path.join(
platformSpecificSassPath,
"/src/sass.dart.snapshot"
)}" "$@"
`,
{
encoding: "utf-8",
mode: 0o777
}
);
}
platformSpecificSassPath,
"/src/sass.dart.snapshot"
)}" "$@"`;

const windows = windows64bit || windows32bit;
const entrypoint = windows ? windowsEntrypoint : linuxAndMacEntrypoint;

fs.writeFileSync(path.join(__dirname, "sass.bat"), entrypoint, {
encoding: "utf-8",
mode: 0o777
});
5 changes: 4 additions & 1 deletion sass-linux-ia32/package.json
Expand Up @@ -12,5 +12,8 @@
],
"cpu": [
"ia32"
]
],
"scripts": {
"postinstall": "node ./postinstall.js"
}
}
23 changes: 23 additions & 0 deletions sass-linux-ia32/postinstall.js
@@ -0,0 +1,23 @@
"use strict";

const { name, os, cpu } = require("./package.json");

if (os && !os.includes(process.platform)) {
throw new Error(
`${name} does not support the platform you are using. You are using: "${
process.platform
}" and ${name} supports: ${os.join(
","
)}. If you think this is a mistake, please contact Origami or open an issue on https://github.com/Financial-Times/sass/issues`
);
}

if (cpu && !cpu.includes(process.arch)) {
throw new Error(
`${name} does not support the cpu you are using. You are using: "${
process.arch
}" and ${name} supports: ${cpu.join(
","
)}. If you think this is a mistake, please contact Origami or open an issue on https://github.com/Financial-Times/sass/issues`
);
}
5 changes: 4 additions & 1 deletion sass-linux-x64/package.json
Expand Up @@ -12,5 +12,8 @@
],
"cpu": [
"x64"
]
],
"scripts": {
"postinstall": "node ./postinstall.js"
}
}
23 changes: 23 additions & 0 deletions sass-linux-x64/postinstall.js
@@ -0,0 +1,23 @@
"use strict";

const { name, os, cpu } = require("./package.json");

if (os && !os.includes(process.platform)) {
throw new Error(
`${name} does not support the platform you are using. You are using: "${
process.platform
}" and ${name} supports: ${os.join(
","
)}. If you think this is a mistake, please contact Origami or open an issue on https://github.com/Financial-Times/sass/issues`
);
}

if (cpu && !cpu.includes(process.arch)) {
throw new Error(
`${name} does not support the cpu you are using. You are using: "${
process.arch
}" and ${name} supports: ${cpu.join(
","
)}. If you think this is a mistake, please contact Origami or open an issue on https://github.com/Financial-Times/sass/issues`
);
}
5 changes: 4 additions & 1 deletion sass-macos-x64/package.json
Expand Up @@ -9,5 +9,8 @@
},
"os": [
"darwin"
]
],
"scripts": {
"postinstall": "node ./postinstall.js"
}
}
23 changes: 23 additions & 0 deletions sass-macos-x64/postinstall.js
@@ -0,0 +1,23 @@
"use strict";

const { name, os, cpu } = require("./package.json");

if (os && !os.includes(process.platform)) {
throw new Error(
`${name} does not support the platform you are using. You are using: "${
process.platform
}" and ${name} supports: ${os.join(
","
)}. If you think this is a mistake, please contact Origami or open an issue on https://github.com/Financial-Times/sass/issues`
);
}

if (cpu && !cpu.includes(process.arch)) {
throw new Error(
`${name} does not support the cpu you are using. You are using: "${
process.arch
}" and ${name} supports: ${cpu.join(
","
)}. If you think this is a mistake, please contact Origami or open an issue on https://github.com/Financial-Times/sass/issues`
);
}
5 changes: 4 additions & 1 deletion sass-windows-ia32/package.json
Expand Up @@ -12,5 +12,8 @@
],
"cpu": [
"ia32"
]
],
"scripts": {
"postinstall": "node ./postinstall.js"
}
}
23 changes: 23 additions & 0 deletions sass-windows-ia32/postinstall.js
@@ -0,0 +1,23 @@
"use strict";

const { name, os, cpu } = require("./package.json");

if (os && !os.includes(process.platform)) {
throw new Error(
`${name} does not support the platform you are using. You are using: "${
process.platform
}" and ${name} supports: ${os.join(
","
)}. If you think this is a mistake, please contact Origami or open an issue on https://github.com/Financial-Times/sass/issues`
);
}

if (cpu && !cpu.includes(process.arch)) {
throw new Error(
`${name} does not support the cpu you are using. You are using: "${
process.arch
}" and ${name} supports: ${cpu.join(
","
)}. If you think this is a mistake, please contact Origami or open an issue on https://github.com/Financial-Times/sass/issues`
);
}
5 changes: 4 additions & 1 deletion sass-windows-x64/package.json
Expand Up @@ -12,5 +12,8 @@
],
"cpu": [
"x64"
]
],
"scripts": {
"postinstall": "node ./postinstall.js"
}
}
23 changes: 23 additions & 0 deletions sass-windows-x64/postinstall.js
@@ -0,0 +1,23 @@
"use strict";

const { name, os, cpu } = require("./package.json");

if (os && !os.includes(process.platform)) {
throw new Error(
`${name} does not support the platform you are using. You are using: "${
process.platform
}" and ${name} supports: ${os.join(
","
)}. If you think this is a mistake, please contact Origami or open an issue on https://github.com/Financial-Times/sass/issues`
);
}

if (cpu && !cpu.includes(process.arch)) {
throw new Error(
`${name} does not support the cpu you are using. You are using: "${
process.arch
}" and ${name} supports: ${cpu.join(
","
)}. If you think this is a mistake, please contact Origami or open an issue on https://github.com/Financial-Times/sass/issues`
);
}

0 comments on commit 79d8224

Please sign in to comment.