Compare commits

..

9 commits

Author SHA1 Message Date
a79e6cca09 readme: Add more information on where they can report missing CI
All checks were successful
/ deploy (push) Successful in 26s
2024-11-22 06:12:55 +00:00
9fb1f9e926 Change from function to boolean return to be compatible with is-ci
All checks were successful
/ deploy (push) Successful in 26s
2024-11-22 05:58:44 +00:00
67da8c7d19 package: Whoops, fix a bug
All checks were successful
/ deploy (push) Successful in 24s
2024-11-22 05:26:41 +00:00
ebb04485c8 readme: Update docs a bit to be clearer
All checks were successful
/ deploy (push) Successful in 26s
2024-11-22 05:13:45 +00:00
58c0b1e613 package: Deploy new minified version 2024-11-22 05:13:30 +00:00
0ef8c6e641 forgejo: Fix ci testing
All checks were successful
/ deploy (push) Successful in 12s
2024-11-22 05:10:06 +00:00
d46e0d834e Minimize it further and clean it up
Some checks failed
/ deploy (push) Failing after 14s
2024-11-22 05:08:16 +00:00
95f00d8700 readme: Add docs for in-ci and not-ci
All checks were successful
/ deploy (push) Successful in 17s
2024-11-19 10:46:15 +00:00
6e6d989a50 Implemented new cli: in-ci and not-ci
All checks were successful
/ deploy (push) Successful in 27s
2024-11-19 10:44:37 +00:00
10 changed files with 89 additions and 53 deletions

View file

@ -16,19 +16,25 @@ jobs:
run: pnpm run test --ignore-only run: pnpm run test --ignore-only
- name: Sanity check it detects this CI - name: Sanity check it detects this CI
run: | run: |
if node index.js; then if node in.js; then
echo "Success" echo "in.js: Success"
else else
echo "Failed for forgejo" echo "Failed for forgejo"
exit 1 exit 1
fi fi
if node not.js; then
echo "Failed for forgejo"
exit 1
else
echo "not.js: Success"
fi
- name: Deply if new version - name: Deply if new version
run: | run: |
echo "" echo ""
echo "------------------------------------" echo "------------------------------------"
echo "" echo ""
mv package.json package-dev.json ./build.sh
mv package-build.json package.json
CURR_VER="$(cat package.json | jq -r .name)_v$(cat package.json | jq -r .version)" 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)" CURR_NAME="$(cat package.json | jq -r .name) v$(cat package.json | jq -r .version)"

View file

@ -1,23 +1,25 @@
# inside-ci # inside-ci
Quick tool to check if we are inside a CI environment 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`.
# API # API
`insideCi()` `const inside = require('inside-ci')`
Returns true if inside CI. Otherwise returns false.
# CLI # CLI
`inside-ci` ## `inside-ci` or `in-ci`
Returns code 0 if inside CI. Otherwise returns an error code of 1. Returns error code 0 if inside CI.
`inside-ci || echo 'We are not inside CI, install some stuff'` `in-ci && echo 'running inside CI'`
`inside-ci && echo 'We are inside CI, install some stuff'` ## `not-ci`
Example: Inverse of the above:
`is-ci || husky install` `not-ci && husky install`

5
build.sh Executable file
View file

@ -0,0 +1,5 @@
#!/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 Executable file
View file

@ -0,0 +1,10 @@
#!/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
View file

@ -1,2 +0,0 @@
/** Returns true if current environment is running inside CI. Otherwise returns false. */
export function insideCi(): boolean

View file

@ -1,14 +0,0 @@
#!/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 Executable file
View file

@ -0,0 +1,2 @@
#!/usr/bin/env node
process.exit(!require("./in.js") ? 0 : 1)

View file

@ -1 +0,0 @@
{"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"]}

View file

@ -1,18 +1,20 @@
{ {
"name": "inside-ci", "name": "inside-ci",
"version": "1.0.1", "version": "2.0.1",
"description": "Quick tool to check if we are inside CI", "description": "Quick tool to check if we are inside CI",
"main": "index.js", "main": "in.js",
"scripts": { "scripts": {
"test": "eltro -r dot test.mjs", "test": "eltro -r dot test.mjs",
"test:watch": "eltro -r dot -w test test.mjs" "test:watch": "eltro -r dot -w test test.mjs"
}, },
"bin": { "bin": {
"inside-ci": "./index.js" "inside-ci": "./in.js",
"in-ci": "./in.js",
"not-ci": "./not.js"
}, },
"watch": { "watch": {
"test": { "test": {
"patterns": ["index.js", "test.mjs"] "patterns": ["in.js", "test.mjs"]
} }
}, },
"repository": { "repository": {
@ -21,9 +23,7 @@
}, },
"keywords": [ "keywords": [
"ci", "ci",
"is-ci", "is-ci"
"inside-ci",
"environment"
], ],
"author": "Jonatan Nilsson", "author": "Jonatan Nilsson",
"license": "WTFPL", "license": "WTFPL",
@ -31,8 +31,8 @@
"eltro": "^1.6.1" "eltro": "^1.6.1"
}, },
"files": [ "files": [
"index.js", "in.js",
"index.d.ts", "not.js",
"README.md", "README.md",
"LICENSE" "LICENSE"
] ]

View file

@ -1,8 +1,9 @@
import { exec } from 'child_process' import { exec } from 'child_process'
import { Eltro as t, assert } from 'eltro' import { Eltro as t, assert } from 'eltro'
import { insideCi } from './index.js' import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
t.describe('#insideCi()', function () { t.describe('in.js', function () {
// Most CI use env.CI (travis, Gitlab, etc.) // Most CI use env.CI (travis, Gitlab, etc.)
// There are some exceptions though: // There are some exceptions though:
// CI_APP_ID is used by Appflow: https://ionic.io/docs/appflow/package/environments#predefined-environments // CI_APP_ID is used by Appflow: https://ionic.io/docs/appflow/package/environments#predefined-environments
@ -23,27 +24,35 @@ t.describe('#insideCi()', 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 => { testVariables.forEach(name => {
t.test(`env.${name} should return true if set`, function () { t.test(`env.${name} should return true if set`, async function () {
process.env[name] = 'asdf' process.env[name] = 'asdf'
assert.ok(insideCi()) assert.ok(await insideCi())
}) })
}) })
t.test('should return false by default', function() { t.test('should return false by default', async function() {
assert.notOk(insideCi()) assert.notOk(await insideCi())
}) })
t.test('should return false if all are empty strings', function () { t.test('should return false if all are empty strings', async function () {
for (let name of testVariables) { for (let name of testVariables) {
process.env[name] = '' process.env[name] = ''
} }
assert.notOk(insideCi()) assert.notOk(await insideCi())
}) })
t.test('should return false if env.CI is specifically "false"', function () { t.test('should return false if env.CI is specifically "false"', async function () {
process.env.CI = 'false' process.env.CI = 'false'
assert.notOk(insideCi()) assert.notOk(await insideCi())
}) })
}) })
@ -57,9 +66,9 @@ function runCommand(command, options) {
}) })
} }
t.describe('CLI', function() { t.describe('CLI in-ci', function() {
t.test('should return success/code 0 if CI is filled', async function () { t.test('should return success/code 0 if CI is filled', async function () {
let result = await runCommand('node index.js', { let result = await runCommand('node in.js', {
env: { CI: 'true' } env: { CI: 'true' }
}) })
@ -67,7 +76,7 @@ t.describe('CLI', function() {
}) })
t.test('should return error code 1 if CI is false', async function () { t.test('should return error code 1 if CI is false', async function () {
let result = await runCommand('node index.js', { let result = await runCommand('node in.js', {
env: { CI: 'false' } env: { CI: 'false' }
}) })
@ -75,3 +84,22 @@ t.describe('CLI', function() {
assert.strictEqual(result.err.code, 1) 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)
})
})