Skip to content

Commit

Permalink
🔀 merge develop to main in preparation for npm publishing (#16)
Browse files Browse the repository at this point in the history
## Description
Adds npm package installer

## Linked issues
Closes #15
  • Loading branch information
xarunoba committed May 24, 2024
2 parents 19a83a1 + eb3a828 commit 84aff19
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 21 deletions.
3 changes: 0 additions & 3 deletions internal/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ var initCmd = &cobra.Command{
if err := os.MkdirAll(config.CcocoDir, 0755); err != nil {
log.Printf("Error creating directory %s: %v", config.PreflightsDir, err)
}
if err := os.MkdirAll(config.CacheDir, 0755); err != nil {
log.Printf("Error creating directory %s: %v", config.PreflightsDir, err)
}
if err := os.MkdirAll(config.ConfigsDir, 0755); err != nil {
log.Printf("Error creating directory %s: %v", config.ConfigsDir, err)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type File struct {
const FileName = "ccoco.config.json"
const CcocoDir = ".ccoco"

const CacheDir = CcocoDir + "/cache"
const ConfigsDir = CcocoDir + "/configs"
const PreflightsDir = CcocoDir + "/preflights"

Expand All @@ -35,9 +34,11 @@ func GetFile() File {
}

if len(configFile.Files) == 0 {
log.Printf("Files are empty. Using default files: %v", DefaultFile.Files)
configFile.Files = DefaultFile.Files
}
if configFile.FilesDir == "" {
log.Printf("FilesDir is empty. Using default filesDir: %v", DefaultFile.FilesDir)
configFile.FilesDir = DefaultFile.FilesDir
}

Expand Down
22 changes: 22 additions & 0 deletions packaging/npm/bin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env node

// Heavily adapted from https://github.com/evilmartians/lefthook/tree/master/packaging/npm-installer

import { spawn } from "child_process";
import path from "path";
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const extension = ["win32", "cygwin"].includes(process.platform) ? ".exe" : "";
const exePath = path.join(__dirname, `ccoco${extension}`);

const command_args = process.argv.slice(2);
const child = spawn(exePath, command_args, { stdio: "inherit" });

child.on("close", (code) => {
if (code !== 0) {
process.exit(1);
}
});
113 changes: 113 additions & 0 deletions packaging/npm/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Heavily adapted from https://github.com/evilmartians/lefthook/tree/master/packaging/npm-installer

import path from "path";
import fs from "fs";
import { DownloaderHelper } from "node-downloader-helper";
import { fileURLToPath } from "url";
import { createRequire } from "module";
import { createGunzip } from "zlib";

const require = createRequire(import.meta.url);
const packageJson = require("./package.json");

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const iswin = ["win32", "cygwin"].includes(process.platform);

async function install() {
if (process.env.CI) {
return;
}
const exePath = await downloadBinary();
if (!iswin) {
fs.chmodSync(exePath, "755");
}
}

function getDownloadURL() {
// Detect OS
// https://nodejs.org/api/process.html#process_process_platform
let goOS = process.platform;
if (iswin) {
goOS = "windows";
}

// Convert the goOS to the os name in the download URL
let downloadOS = goOS === "darwin" ? "macOS" : goOS;
downloadOS = `${downloadOS.charAt(0).toUpperCase()}${downloadOS.slice(1)}`;

// Detect architecture
// https://nodejs.org/api/process.html#process_process_arch
let arch = process.arch;
switch (process.arch) {
case "x64": {
arch = "x86_64";
break;
}
case "x32":
case "ia32": {
arch = "i386";
break;
}
}
const version = packageJson.version;

return `https://github.com/xarunoba/ccoco/releases/download/v${version}/ccoco_${version}_${downloadOS}_${arch}.gz`;
}

async function downloadBinary() {
const downloadURL = getDownloadURL();
const fileName = `ccoco${iswin ? ".exe.gz" : ".gz"}`;
const binDir = path.join(__dirname, "bin");
const compressedFilePath = path.join(binDir, fileName);
const decompressedFileName = `ccoco${iswin ? ".exe" : ""}`;
const decompressedFilePath = path.join(binDir, decompressedFileName);

const dl = new DownloaderHelper(downloadURL, binDir, {
fileName,
retry: { maxRetries: 5, delay: 50 },
});

dl.on("end", async () => {
console.log("ccoco gzip was downloaded");
try {
await decompressFile(compressedFilePath, decompressedFilePath);
fs.unlinkSync(compressedFilePath); // remove compressed file
console.log("ccoco binary was decompressed");
} catch (e) {
console.error(`Failed to decompress ${fileName}: ${e.message}`);
throw new Error(`Failed to decompress ${fileName}: ${e.message}`);
}
});

try {
await dl.start();
} catch (e) {
const message = `Failed to download ${fileName}: ${e.message} while fetching ${downloadURL}`;
console.error(message);
throw new Error(message);
}
return decompressedFilePath;
}

async function decompressFile(sourcePath, destinationPath) {
return new Promise((resolve, reject) => {
const gunzip = createGunzip();
const source = fs.createReadStream(sourcePath);
const destination = fs.createWriteStream(destinationPath);

source
.pipe(gunzip)
.pipe(destination)
.on("finish", resolve)
.on("error", reject);
});
}

// start:
try {
await install();
} catch (e) {
throw e;
}
8 changes: 5 additions & 3 deletions packaging/npm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xarunoba/ccoco",
"version": "0.0.0-ccoco-versioner",
"version": "0.6.2",
"description": "Change configs on checkout.",
"main": "index.js",
"repository": {
Expand All @@ -13,7 +13,7 @@
"ccoco": "./bin/index.js"
},
"scripts": {
"postinstall": "node postinstall.js",
"install": "node install.js",
"prepare": "npx simple-git-hooks"
},
"author": "Jeter Jude C. Santos <me@xaru.win> (https://xaru.win)",
Expand All @@ -40,9 +40,11 @@
"cpu": [
"x64",
"arm64",
"ia32"
"ia32",
"x32"
],
"dependencies": {
"node-downloader-helper": "^2.1.9",
"yargs": "^17.7.2"
},
"devDependencies": {
Expand Down
Empty file removed packaging/npm/postinstall.js
Empty file.
16 changes: 10 additions & 6 deletions taskfile.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ tasks:
cmds:
- npm {{.CLI_ARGS}}

release-*:
vars:
VERSION: '{{index .MATCH 0}}'
release:
requires:
vars: [VERSION]
cmds:
- bash ./versionmanager {{.VERSION}}
- 'sed -i "s/var Version = ".*"/var Version = \"{{.VERSION}}\"/" ./internal/version/version.go'
- 'sed -i "s/\"version\": ".*",/\"version\": \"{{.VERSION}}\",/" ./packaging/npm/package.json'
- git add ./internal/version/version.go
- git commit -m "🔖 release version {{.VERSION}}"
- git tag {{index .MATCH 0}}
- git add ./packaging/npm/package.json
# - git commit -m "🔖 release version {{.VERSION}}"
# - git tag {{.VERSION}}
# - git push origin
# - git push origin --tags
# - task npm -- publish
preconditions:
- test "$(git rev-parse --abbrev-ref HEAD)" = "main"
# - task npm -- publish --dry-run
8 changes: 0 additions & 8 deletions versionmanager

This file was deleted.

0 comments on commit 84aff19

Please sign in to comment.