Skip to content

Commit ceec8c7

Browse files
added npm-package
1 parent c50d44f commit ceec8c7

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
.vscode
22
target
33
Cargo.lock
4-
npm-package
4+
npm-package/src/bin

npm-package/package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "next-css-linter",
3+
"version": "1.0.2",
4+
"description": "Linter for NextJs CSS modules",
5+
"main": "index.js",
6+
"scripts": {
7+
"postinstall": "node src/install.js"
8+
},
9+
"author": "AndcoolSystems",
10+
"license": "MIT",
11+
"os": [
12+
"darwin",
13+
"linux",
14+
"win32"
15+
],
16+
"files": [
17+
"bin/",
18+
"bin-wrapper.js",
19+
"install.js"
20+
],
21+
"bin": {
22+
"css-linter": "./src/bin-wrapper.js"
23+
}
24+
}

npm-package/src/bin-wrapper.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env node
2+
const { join } = require("path");
3+
const { execFileSync } = require("child_process");
4+
const os = require("os");
5+
6+
const binaries = {
7+
linux: "css-linter-linux",
8+
darwin: "css-linter-macos",
9+
win32: "css-linter-win.exe",
10+
};
11+
12+
const platform = os.platform();
13+
const binary = binaries[platform];
14+
15+
if (!binary) {
16+
console.error(`Unsupported platform: ${platform}`);
17+
process.exit(1);
18+
}
19+
20+
const binaryPath = join(__dirname, "bin", binary);
21+
22+
try {
23+
execFileSync(binaryPath, process.argv.slice(2), { stdio: 'inherit' });
24+
} catch (err) {
25+
console.error(err.message);
26+
process.exit(1);
27+
}

npm-package/src/install.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const { writeFileSync, chmodSync, mkdirSync } = require("fs");
2+
const { join } = require("path");
3+
const { get } = require("https");
4+
const os = require("os");
5+
6+
const binaries = {
7+
linux: "css-linter-linux",
8+
darwin: "css-linter-macos",
9+
win32: "css-linter-win.exe",
10+
};
11+
12+
const urls = {
13+
linux: "https://github.com/Andcool-Systems/css-linter/releases/latest/download/css-linter-linux",
14+
darwin: "https://github.com/Andcool-Systems/css-linter/releases/latest/download/css-linter-macos",
15+
win32: "https://github.com/Andcool-Systems/css-linter/releases/latest/download/css-linter-win.exe",
16+
};
17+
18+
const platform = os.platform();
19+
const binary = binaries[platform];
20+
const url = urls[platform];
21+
22+
if (!binary || !url) {
23+
console.error(`Unsupported platform: ${platform}`);
24+
process.exit(1);
25+
}
26+
27+
const binPath = join(__dirname, "bin", binary);
28+
mkdirSync(join(__dirname, "bin"), { recursive: true });
29+
30+
const downloadFile = (url, binPath) => {
31+
get(url, (res) => {
32+
if (res.statusCode === 302 || res.statusCode === 301) {
33+
const redirectUrl = res.headers.location;
34+
console.log(`Redirecting to ${redirectUrl}`);
35+
downloadFile(redirectUrl, binPath);
36+
return;
37+
}
38+
39+
if (res.statusCode !== 200) {
40+
console.error(`Failed to download binary: ${res.statusCode}`);
41+
process.exit(1);
42+
}
43+
44+
const file = [];
45+
res.on("data", (chunk) => file.push(chunk));
46+
res.on("end", () => {
47+
writeFileSync(binPath, Buffer.concat(file));
48+
chmodSync(binPath, 0o755);
49+
console.log(`Binary installed: ${binPath}`);
50+
});
51+
}).on('error', (err) => {
52+
console.error(`Error during download: ${err.message}`);
53+
process.exit(1);
54+
});
55+
}
56+
57+
downloadFile(url, binPath);

0 commit comments

Comments
 (0)