Compare commits
No commits in common. "master" and "inside-ci_v1.0.1" have entirely different histories.
master
...
inside-ci_
10 changed files with 53 additions and 89 deletions
|
@ -16,25 +16,19 @@ jobs:
|
|||
run: pnpm run test --ignore-only
|
||||
- name: Sanity check it detects this CI
|
||||
run: |
|
||||
if node in.js; then
|
||||
echo "in.js: Success"
|
||||
if node index.js; then
|
||||
echo "Success"
|
||||
else
|
||||
echo "Failed for forgejo"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if node not.js; then
|
||||
echo "Failed for forgejo"
|
||||
exit 1
|
||||
else
|
||||
echo "not.js: Success"
|
||||
fi
|
||||
- name: Deply if new version
|
||||
run: |
|
||||
echo ""
|
||||
echo "------------------------------------"
|
||||
echo ""
|
||||
./build.sh
|
||||
mv package.json package-dev.json
|
||||
mv package-build.json package.json
|
||||
|
||||
CURR_VER="$(cat package.json | jq -r .name)_v$(cat package.json | jq -r .version)"
|
||||
CURR_NAME="$(cat package.json | jq -r .name) v$(cat package.json | jq -r .version)"
|
||||
|
|
22
README.md
22
README.md
|
@ -1,25 +1,23 @@
|
|||
# inside-ci
|
||||
|
||||
Returns true if code is running inside CI, otherwise false.
|
||||
|
||||
This also works as a drop-in replacement for anyone currently using `is-ci`.
|
||||
|
||||
If your CI is not being detected, feel free to [add an issue for it](https://git.nfp.is/TheThing/inside-ci/issues) or hit me up on [X (formerly Twitter)](https://x.com/TheThing89) or on discord as `thething_89`.
|
||||
Quick tool to check if we are inside a CI environment
|
||||
|
||||
# API
|
||||
|
||||
`const inside = require('inside-ci')`
|
||||
`insideCi()`
|
||||
|
||||
Returns true if inside CI. Otherwise returns false.
|
||||
|
||||
# CLI
|
||||
|
||||
## `inside-ci` or `in-ci`
|
||||
`inside-ci`
|
||||
|
||||
Returns error code 0 if inside CI.
|
||||
Returns code 0 if inside CI. Otherwise returns an error code of 1.
|
||||
|
||||
`in-ci && echo 'running inside CI'`
|
||||
`inside-ci || echo 'We are not inside CI, install some stuff'`
|
||||
|
||||
## `not-ci`
|
||||
`inside-ci && echo 'We are inside CI, install some stuff'`
|
||||
|
||||
Inverse of the above:
|
||||
Example:
|
||||
|
||||
`not-ci && husky install`
|
||||
`is-ci || husky install`
|
||||
|
|
5
build.sh
5
build.sh
|
@ -1,5 +0,0 @@
|
|||
#!/bin/bash
|
||||
|
||||
mv package.json package.json.backup
|
||||
|
||||
jq -c 'pick(.name, .version, .description, .main, .bin, .repository, .keywords, .author, .license, .files)' package.json.backup > package.json
|
10
in.js
10
in.js
|
@ -1,10 +0,0 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
// Bail out if CI is overrided
|
||||
let is = module.exports = process.env.CI === 'false'
|
||||
? false
|
||||
: !!['CI','CI_APP_ID','BUILD_NUMBER','CI_NAME','RUN_ID'].some(x => process.env[x])
|
||||
|
||||
if (require.main === module) {
|
||||
process.exit(is ? 0 : 1)
|
||||
}
|
2
index.d.ts
vendored
Normal file
2
index.d.ts
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
/** Returns true if current environment is running inside CI. Otherwise returns false. */
|
||||
export function insideCi(): boolean
|
14
index.js
Executable file
14
index.js
Executable file
|
@ -0,0 +1,14 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
function insideCi() {
|
||||
// Bail out if this is specifically overwritten to false.
|
||||
if (process.env.CI === 'false') return false
|
||||
|
||||
return !!['CI','CI_APP_ID','BUILD_NUMBER','CI_NAME','RUN_ID'].some(x => process.env[x])
|
||||
}
|
||||
|
||||
module.exports.insideCi = insideCi
|
||||
|
||||
if (require.main === module) {
|
||||
process.exit(insideCi() ? 0 : 1)
|
||||
}
|
2
not.js
2
not.js
|
@ -1,2 +0,0 @@
|
|||
#!/usr/bin/env node
|
||||
process.exit(!require("./in.js") ? 0 : 1)
|
1
package-build.json
Normal file
1
package-build.json
Normal file
|
@ -0,0 +1 @@
|
|||
{"name":"inside-ci","version":"1.0.1","description":"Quick tool to check if we are inside CI","main":"index.js","bin":{"inside-ci":"./index.js"},"repository":{"type":"git","url":"https://git.nfp.is/TheThing/inside-ci.git"},"keywords":["ci","is-ci","inside-ci","environment"],"author":"Jonatan Nilsson","license":"WTFPL","files":["index.js","index.d.ts","README.md","LICENSE"]}
|
18
package.json
18
package.json
|
@ -1,20 +1,18 @@
|
|||
{
|
||||
"name": "inside-ci",
|
||||
"version": "2.0.1",
|
||||
"version": "1.0.1",
|
||||
"description": "Quick tool to check if we are inside CI",
|
||||
"main": "in.js",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "eltro -r dot test.mjs",
|
||||
"test:watch": "eltro -r dot -w test test.mjs"
|
||||
},
|
||||
"bin": {
|
||||
"inside-ci": "./in.js",
|
||||
"in-ci": "./in.js",
|
||||
"not-ci": "./not.js"
|
||||
"inside-ci": "./index.js"
|
||||
},
|
||||
"watch": {
|
||||
"test": {
|
||||
"patterns": ["in.js", "test.mjs"]
|
||||
"patterns": ["index.js", "test.mjs"]
|
||||
}
|
||||
},
|
||||
"repository": {
|
||||
|
@ -23,7 +21,9 @@
|
|||
},
|
||||
"keywords": [
|
||||
"ci",
|
||||
"is-ci"
|
||||
"is-ci",
|
||||
"inside-ci",
|
||||
"environment"
|
||||
],
|
||||
"author": "Jonatan Nilsson",
|
||||
"license": "WTFPL",
|
||||
|
@ -31,8 +31,8 @@
|
|||
"eltro": "^1.6.1"
|
||||
},
|
||||
"files": [
|
||||
"in.js",
|
||||
"not.js",
|
||||
"index.js",
|
||||
"index.d.ts",
|
||||
"README.md",
|
||||
"LICENSE"
|
||||
]
|
||||
|
|
54
test.mjs
54
test.mjs
|
@ -1,9 +1,8 @@
|
|||
import { exec } from 'child_process'
|
||||
import { Eltro as t, assert } from 'eltro'
|
||||
import { createRequire } from 'node:module';
|
||||
const require = createRequire(import.meta.url);
|
||||
import { insideCi } from './index.js'
|
||||
|
||||
t.describe('in.js', function () {
|
||||
t.describe('#insideCi()', function () {
|
||||
// Most CI use env.CI (travis, Gitlab, etc.)
|
||||
// There are some exceptions though:
|
||||
// CI_APP_ID is used by Appflow: https://ionic.io/docs/appflow/package/environments#predefined-environments
|
||||
|
@ -24,35 +23,27 @@ t.describe('in.js', function () {
|
|||
}
|
||||
})
|
||||
|
||||
let n = 1
|
||||
function insideCi() {
|
||||
Object.keys(require.cache).forEach(key => {
|
||||
delete require.cache[key]
|
||||
})
|
||||
return import(`./in.js?${n++}`).then(x => x.default)
|
||||
}
|
||||
|
||||
testVariables.forEach(name => {
|
||||
t.test(`env.${name} should return true if set`, async function () {
|
||||
t.test(`env.${name} should return true if set`, function () {
|
||||
process.env[name] = 'asdf'
|
||||
assert.ok(await insideCi())
|
||||
assert.ok(insideCi())
|
||||
})
|
||||
})
|
||||
|
||||
t.test('should return false by default', async function() {
|
||||
assert.notOk(await insideCi())
|
||||
t.test('should return false by default', function() {
|
||||
assert.notOk(insideCi())
|
||||
})
|
||||
|
||||
t.test('should return false if all are empty strings', async function () {
|
||||
t.test('should return false if all are empty strings', function () {
|
||||
for (let name of testVariables) {
|
||||
process.env[name] = ''
|
||||
}
|
||||
assert.notOk(await insideCi())
|
||||
assert.notOk(insideCi())
|
||||
})
|
||||
|
||||
t.test('should return false if env.CI is specifically "false"', async function () {
|
||||
t.test('should return false if env.CI is specifically "false"', function () {
|
||||
process.env.CI = 'false'
|
||||
assert.notOk(await insideCi())
|
||||
assert.notOk(insideCi())
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -66,9 +57,9 @@ function runCommand(command, options) {
|
|||
})
|
||||
}
|
||||
|
||||
t.describe('CLI in-ci', function() {
|
||||
t.describe('CLI', function() {
|
||||
t.test('should return success/code 0 if CI is filled', async function () {
|
||||
let result = await runCommand('node in.js', {
|
||||
let result = await runCommand('node index.js', {
|
||||
env: { CI: 'true' }
|
||||
})
|
||||
|
||||
|
@ -76,7 +67,7 @@ t.describe('CLI in-ci', function() {
|
|||
})
|
||||
|
||||
t.test('should return error code 1 if CI is false', async function () {
|
||||
let result = await runCommand('node in.js', {
|
||||
let result = await runCommand('node index.js', {
|
||||
env: { CI: 'false' }
|
||||
})
|
||||
|
||||
|
@ -84,22 +75,3 @@ t.describe('CLI in-ci', function() {
|
|||
assert.strictEqual(result.err.code, 1)
|
||||
})
|
||||
})
|
||||
|
||||
t.describe('CLI not-ci', function() {
|
||||
t.test('should return error code 1 if CI is filled', async function () {
|
||||
let result = await runCommand('node not.js', {
|
||||
env: { CI: 'true' }
|
||||
})
|
||||
|
||||
assert.ok(result.err)
|
||||
assert.strictEqual(result.err.code, 1)
|
||||
})
|
||||
|
||||
t.test('should return success/code 0 if CI is false', async function () {
|
||||
let result = await runCommand('node not.js', {
|
||||
env: { CI: 'false' }
|
||||
})
|
||||
|
||||
assert.notOk(result.err)
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue