Skip to content

Commit

Permalink
feat!: v3 rewrite (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Nov 2, 2021
1 parent cb2c21d commit 7a3d4fb
Show file tree
Hide file tree
Showing 11 changed files with 867 additions and 172 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: ci

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
ci:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'
cache: yarn
- run: yarn install
- run: yarn prepack
- run: yarn test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
*.log*
*.log*
dist
59 changes: 31 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,51 @@
# std-env

[![npm](https://img.shields.io/npm/dt/std-env.svg?style=flat-square)](http://npmjs.com/package/std-env)
[![npm](https://img.shields.io/npm/dm/std-env.svg?style=flat-square)](http://npmjs.com/package/std-env)
[![npm](https://img.shields.io/npm/v/std-env.svg?style=flat-square)](http://npmjs.com/package/std-env)

Detect running environment of the current Node.js process.
> Simplified way to detect current running Javascript environment
## Installation

Using Yarn:

```
```sh
# Using Yarn
yarn add std-env
```

Using npm:

```
# Using npm
npm i std-env
```

## Usage

```js
const env = require('std-env')

console.log(env)

/*
{
browser: false,
test: false,
dev: true,
production: false,
debug: false,
ci: false,
tty: true,
minimalCLI: false,
windows: false,
darwin: true,
linux: false
}
*/
// ESM
import { isWindows } from 'std-env'

// CommonJS
const { isCI } = require('std-env')
```

Available exports:

- `hasTTY`
- `hasWindow`
- `isCI`
- `isDebug`
- `isDevelopment`
- `isLinux`
- `isMacOS`
- `isMinimal`
- `isProduction`
- `isTest`
- `isWindows`
- `platform`
- `provider`

You can read more about how each flag works from [./src/index.ts](./src/index.ts).

List of well known providers can be found from [./src/providers.ts](./src/providers.ts).


## License

MIT
9 changes: 9 additions & 0 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
declaration: true,
emitCJS: true,
entries: [
'src/index'
]
})
20 changes: 0 additions & 20 deletions index.d.ts

This file was deleted.

70 changes: 0 additions & 70 deletions index.js

This file was deleted.

24 changes: 14 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
{
"name": "std-env",
"version": "2.3.1",
"description": "Detect running environment of the current Node.js process",
"description": "Simplified way to detect current running Javascript environment",
"repository": "unjs/std-env",
"license": "MIT",
"main": "./index.js",
"types": "./index.d.ts",
"scripts": {
"release": "standard-version && git push --follow-tags && npm publish"
"sideEffects": false,
"exports": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"main": "./index.cjs",
"types": "./index.d.ts",
"files": [
"index.js",
"index.d.ts"
"dist"
],
"dependencies": {
"ci-info": "^3.1.1"
"scripts": {
"prepack": "unbuild",
"test": "node test.cjs",
"release": "yarn test && standard-version && git push --follow-tags && npm publish"
},
"devDependencies": {
"standard-version": "^9.3.1"
"standard-version": "^9.3.2",
"unbuild": "^0.5.11"
}
}
53 changes: 53 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { detectProvider } from './providers'

export type { ProviderName, ProviderInfo } from './providers'

const processShim = typeof process !== 'undefined' ? process : {} as typeof process
const envShim = processShim.env || {}
const providerInfo = detectProvider(envShim)
const nodeENV = envShim.NODE_ENV || ''

/** Value of process.platform */
export const platform = processShim.platform

/** Current current provider name */
export const provider = providerInfo.name

/** Detect if `CI` environment variable is set or a provider CI detected */
export const isCI = toBoolean(envShim.CI) || providerInfo.ci !== false

/** Detect if stdout.TTY is available */
export const hasTTY = toBoolean(processShim.stdout && processShim.stdout.isTTY)

/** Detect if global `window` object is available */
export const hasWindow = typeof window !== 'undefined'

/** Detect if `DEBUG` environment variable is set */
export const isDebug = toBoolean(envShim.DEBUG)

/** Detect if `NODE_ENV` environment variable is `test` */
export const isTest = toBoolean(envShim.TEST)

/** Detect if `NODE_ENV` environment variable is `production` */
export const isProduction = nodeENV === 'production'

/** Detect if `NODE_ENV` environment variable is `dev` or `development` */
export const isDevelopment = nodeENV === 'dev' || nodeENV === 'development'

/** Detect if MINIMAL environment variable is set, running in CI or test or TTY is unavailable */
export const isMinimal = toBoolean(envShim.MINIMAL) || isCI || isTest || !hasTTY

/** Detect if process.platform is Windows */
export const isWindows = /^win/i.test(platform)

/** Detect if process.platform is Linux */
export const isLinux = /^linux/i.test(platform)

/** Detect if process.platform is macOS (darwin kernel) */
export const isMacOS = /^darwin/i.test(platform)

// -- Utils --

function toBoolean(val) {
return val ? (val !== 'false') : false
}
73 changes: 73 additions & 0 deletions src/providers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Reference: https://github.com/watson/ci-info/blob/v3.2.0/vendors.json

type InternalProvider = [vendorName: string, envName?: string, meta?: Record<string, any>]

const providers: InternalProvider[] = [
["APPVEYOR"],
["AZURE_PIPELINES", "SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"],
["AZURE_STATIC", "INPUT_AZURE_STATIC_WEB_APPS_API_TOKEN"],
["APPCIRCLE", "AC_APPCIRCLE"],
["BAMBOO", "bamboo_planKey"],
["BITBUCKET", "BITBUCKET_COMMIT"],
["BITRISE", "BITRISE_IO"],
["BUDDY", "BUDDY_WORKSPACE_ID"],
["BUILDKITE"],
["CIRCLE", "CIRCLECI"],
["CIRRUS", "CIRRUS_CI"],
["CODEBUILD", "CODEBUILD_BUILD_ARN"],
["CODEFRESH", "CF_BUILD_ID"],
["DRONE"],
["DRONE", "DRONE_BUILD_EVENT"],
["DSARI"],
["GITHUB_ACTIONS"],
["GITLAB", "GITLAB_CI"],
["GITLAB", "CI_MERGE_REQUEST_ID"],
["GOCD", "GO_PIPELINE_LABEL"],
["LAYERCI"],
["HUDSON", "HUDSON_URL"],
["JENKINS", "JENKINS_URL"],
["MAGNUM"],
["NETLIFY"],
["NETLIFY", "NETLIFY_LOCAL", { ci: false }],
["NEVERCODE"],
["RENDER"],
["SAIL", "SAILCI"],
["SEMAPHORE"],
["SCREWDRIVER"],
["SHIPPABLE"],
["SOLANO", "TDDIUM"],
["STRIDER"],
["TEAMCITY", "TEAMCITY_VERSION"],
["TRAVIS"],
["VERCEL", "NOW_BUILDER"],
["APPCENTER", "APPCENTER_BUILD_ID"],
["CODESANDBOX", "CODESANDBOX_SSE", { ci: false }],
["STACKBLITZ"],
]

export type ProviderName = Lowercase<(typeof providers)[number][0]>
export type ProviderInfo = { name: ProviderName, [meta: string]: any }

export function detectProvider(env: Record<string, string>): ProviderInfo | null {
// Based on env
for (const provider of providers) {
const envName = provider[1] || provider[0]
if (env[envName]) {
return {
name: provider[0].toLowerCase(),
...provider[2] as any
}
}
}

// Stackblitz / Webcontainer
if (env.SHELL && env.SHELL === '/bin/jsh') {
return {
name: 'STACKBLITZ'
}
}

return {
name: ''
}
}
2 changes: 2 additions & 0 deletions test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const envInfo = require('jiti')(__dirname)('./src/index.ts')
console.log(envInfo)
Loading

0 comments on commit 7a3d4fb

Please sign in to comment.