Skip to content

Commit

Permalink
feat: update package generate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
shaobeichen committed Oct 30, 2024
1 parent fddb1d5 commit 38df7e7
Show file tree
Hide file tree
Showing 17 changed files with 352 additions and 47 deletions.
47 changes: 47 additions & 0 deletions .ls
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
aix/ppc64
android/386
android/amd64
android/arm
android/arm64
darwin/amd64
darwin/arm64
dragonfly/amd64
freebsd/386
freebsd/amd64
freebsd/arm
freebsd/arm64
freebsd/riscv64
illumos/amd64
ios/amd64
ios/arm64
js/wasm
linux/386
linux/amd64
linux/arm
linux/arm64
linux/loong64
linux/mips
linux/mips64
linux/mips64le
linux/mipsle
linux/ppc64
linux/ppc64le
linux/riscv64
linux/s390x
netbsd/386
netbsd/amd64
netbsd/arm
netbsd/arm64
openbsd/386
openbsd/amd64
openbsd/arm
openbsd/arm64
openbsd/mips64
plan9/386
plan9/amd64
plan9/arm
solaris/amd64
windows/386
windows/amd64
windows/arm
windows/arm64
99 changes: 99 additions & 0 deletions install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import https from 'https'

function installUsingNPM(pkg, subpath, binPath) {
const env = { ...process.env, npm_config_global: void 0 }
const esbuildLibDir = path2.dirname(require.resolve('esbuild'))
const installDir = path2.join(esbuildLibDir, 'npm-install')
fs2.mkdirSync(installDir)
try {
fs2.writeFileSync(path2.join(installDir, 'package.json'), '{}')
child_process.execSync(
`npm install --loglevel=error --prefer-offline --no-audit --progress=false ${pkg}@${versionFromPackageJSON}`,
{ cwd: installDir, stdio: 'pipe', env },
)
const installedBinPath = path2.join(installDir, 'node_modules', pkg, subpath)
fs2.renameSync(installedBinPath, binPath)
} finally {
try {
removeRecursive(installDir)
} catch {}
}
}

function removeRecursive(dir) {
for (const entry of fs2.readdirSync(dir)) {
const entryPath = path2.join(dir, entry)
let stats
try {
stats = fs2.lstatSync(entryPath)
} catch {
continue
}
if (stats.isDirectory()) removeRecursive(entryPath)
else fs2.unlinkSync(entryPath)
}
fs2.rmdirSync(dir)
}

async function downloadDirectlyFromNPM(pkg, subpath, binPath) {
const url = `https://registry.npmjs.org/${pkg}/-/${pkg.replace(
'@esbuild/',
'',
)}-${versionFromPackageJSON}.tgz`
console.error(`[esbuild] Trying to download ${JSON.stringify(url)}`)
try {
fs2.writeFileSync(binPath, extractFileFromTarGzip(await fetch(url), subpath))
fs2.chmodSync(binPath, 493)
} catch (e) {
console.error(`[esbuild] Failed to download ${JSON.stringify(url)}: ${(e && e.message) || e}`)
throw e
}
}

async function checkAndPreparePackage() {
if (isValidBinaryPath(ESBUILD_BINARY_PATH)) {
if (!fs2.existsSync(ESBUILD_BINARY_PATH)) {
console.warn(
`[esbuild] Ignoring bad configuration: ESBUILD_BINARY_PATH=${ESBUILD_BINARY_PATH}`,
)
} else {
applyManualBinaryPathOverride(ESBUILD_BINARY_PATH)
return
}
}
const { pkg, subpath } = pkgAndSubpathForCurrentPlatform()
let binPath
try {
binPath = require.resolve(`${pkg}/${subpath}`)
} catch (e) {
console.error(`[esbuild] Failed to find package "${pkg}" on the file system
This can happen if you use the "--no-optional" flag. The "optionalDependencies"
package.json feature is used by esbuild to install the correct binary executable
for your current platform. This install script will now attempt to work around
this. If that fails, you need to remove the "--no-optional" flag to use esbuild.
`)
binPath = downloadedBinPath(pkg, subpath)
try {
console.error(`[esbuild] Trying to install package "${pkg}" using npm`)
installUsingNPM(pkg, subpath, binPath)
} catch (e2) {
console.error(
`[esbuild] Failed to install package "${pkg}" using npm: ${(e2 && e2.message) || e2}`,
)
try {
await downloadDirectlyFromNPM(pkg, subpath, binPath)
} catch (e3) {
throw new Error(`Failed to install package "${pkg}"`)
}
}
}
maybeOptimizePackage(binPath)
}
checkAndPreparePackage().then(() => {
if (isToPathJS) {
validateBinaryVersion(process.execPath, toPath)
} else {
validateBinaryVersion(toPath)
}
})
Binary file added npm/darwin-amd64/grprogress-darwin-amd64
Binary file not shown.
34 changes: 34 additions & 0 deletions npm/darwin-amd64/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@grprogress/darwin-amd64",
"version": "0.0.0",
"description": "a gradient progress for cli",
"main": "grprogress-darwin-amd64",
"repository": {
"type": "git",
"url": "git+https://github.com/shaobeichen/grprogress.git"
},
"keywords": [
"github",
"go",
"cli",
"gradient",
"progress",
"gradient",
"bubbletea",
"bubble"
],
"homepage": "https://github.com/shaobeichen/grprogress#readme",
"publishConfig": {
"access": "public",
"provenance": true
},
"files": [
"grprogress-darwin-amd64"
],
"os": [
"darwin"
],
"cpu": [
"amd64"
]
}
Binary file added npm/darwin-arm64/grprogress-darwin-arm64
Binary file not shown.
34 changes: 34 additions & 0 deletions npm/darwin-arm64/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@grprogress/darwin-arm64",
"version": "0.0.0",
"description": "a gradient progress for cli",
"main": "grprogress-darwin-arm64",
"repository": {
"type": "git",
"url": "git+https://github.com/shaobeichen/grprogress.git"
},
"keywords": [
"github",
"go",
"cli",
"gradient",
"progress",
"gradient",
"bubbletea",
"bubble"
],
"homepage": "https://github.com/shaobeichen/grprogress#readme",
"publishConfig": {
"access": "public",
"provenance": true
},
"files": [
"grprogress-darwin-arm64"
],
"os": [
"darwin"
],
"cpu": [
"arm64"
]
}
Binary file added npm/linux-amd64/grprogress-linux-amd64
Binary file not shown.
34 changes: 34 additions & 0 deletions npm/linux-amd64/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@grprogress/linux-amd64",
"version": "0.0.0",
"description": "a gradient progress for cli",
"main": "grprogress-linux-amd64",
"repository": {
"type": "git",
"url": "git+https://github.com/shaobeichen/grprogress.git"
},
"keywords": [
"github",
"go",
"cli",
"gradient",
"progress",
"gradient",
"bubbletea",
"bubble"
],
"homepage": "https://github.com/shaobeichen/grprogress#readme",
"publishConfig": {
"access": "public",
"provenance": true
},
"files": [
"grprogress-linux-amd64"
],
"os": [
"linux"
],
"cpu": [
"amd64"
]
}
20 changes: 0 additions & 20 deletions npm/package.json.tmpl

This file was deleted.

Binary file added npm/windows-amd64/grprogress-windows-amd64.exe
Binary file not shown.
34 changes: 34 additions & 0 deletions npm/windows-amd64/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@grprogress/windows-amd64",
"version": "0.0.0",
"description": "a gradient progress for cli",
"main": "grprogress-windows-amd64.exe",
"repository": {
"type": "git",
"url": "git+https://github.com/shaobeichen/grprogress.git"
},
"keywords": [
"github",
"go",
"cli",
"gradient",
"progress",
"gradient",
"bubbletea",
"bubble"
],
"homepage": "https://github.com/shaobeichen/grprogress#readme",
"publishConfig": {
"access": "public",
"provenance": true
},
"files": [
"grprogress-windows-amd64.exe"
],
"os": [
"windows"
],
"cpu": [
"amd64"
]
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"type": "module",
"main": "./src/index.js",
"scripts": {
"postinstall": "node install.js",
"dev": "node ./src/dev.js",
"dev:go": "go run . 0.5",
"build:go": "sh ./scripts/build.sh"
"build:go": "node ./scripts/build.js"
},
"keywords": [],
"author": "shaobeichen"
Expand Down
34 changes: 34 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { exec } from 'child_process'

const platforms = ['darwin/arm64', 'darwin/amd64', 'linux/amd64', 'windows/amd64']

platforms.forEach((platform) => {
const [os, arch] = platform.split('/')
let output = `npm/${os}-${arch}/grprogress-${os}-${arch}`

if (os === 'windows') output += '.exe'

const command = `go build -o ${output} main.go`

exec(
command,
{
env: {
...process.env,
GOOS: os,
GOARCH: arch,
},
},
(error, stdout, stderr) => {
if (error) {
console.error(`error ${platform}: ${error.message}`)
return
}
if (stderr) {
console.error(`stderr: ${stderr}`)
return
}
console.log(`success ${platform}: ${stdout}`)
},
)
})
18 changes: 0 additions & 18 deletions scripts/build.sh

This file was deleted.

File renamed without changes.
6 changes: 1 addition & 5 deletions src/dev.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import { update } from './index.js'

update(0.5)

setTimeout(() => {
update(1)
}, 2000)
update(1)
Loading

0 comments on commit 38df7e7

Please sign in to comment.