Skip to content

Commit 9283519

Browse files
chore(internal): refactor scripts (openai#806)
1 parent 3dabda8 commit 9283519

16 files changed

+82
-80
lines changed

bin/check-test-server

-50
This file was deleted.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
"private": false,
1616
"scripts": {
1717
"test": "./scripts/test",
18-
"build": "bash ./build",
18+
"build": "./scripts/build",
1919
"prepack": "echo 'to pack, run yarn build && (cd dist; yarn pack)' && exit 1",
2020
"prepublishOnly": "echo 'to publish, run yarn build && (cd dist; yarn publish)' && exit 1",
2121
"format": "prettier --write --cache --cache-strategy metadata . !dist",
22-
"prepare": "if ./scripts/check-is-in-git-install.sh; then npm run build; fi",
22+
"prepare": "if ./scripts/utils/check-is-in-git-install.sh; then ./scripts/build; fi",
2323
"tsn": "ts-node -r tsconfig-paths/register",
24-
"lint": "eslint --ext ts,js .",
24+
"lint": "./scripts/lint",
2525
"fix": "eslint --fix --ext ts,js ."
2626
},
2727
"dependencies": {

release-please-config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@
6363
"extra-files": [
6464
"src/version.ts",
6565
"README.md",
66-
"build-deno"
66+
"./scripts/build-deno"
6767
]
6868
}

scripts/bootstrap

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
cd "$(dirname "$0")/.."
6+
7+
PACKAGE_MANAGER=$(command -v yarn >/dev/null 2>&1 && echo "yarn" || echo "npm")
8+
9+
$PACKAGE_MANAGER install

build scripts/build

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#!/usr/bin/env bash
2+
23
set -exuo pipefail
34

4-
node scripts/check-version.cjs
5+
cd "$(dirname "$0")/.."
6+
7+
node scripts/utils/check-version.cjs
58

69
# Build into dist and will publish the package from there,
710
# so that src/resources/foo.ts becomes <package root>/resources/foo.js
@@ -22,7 +25,7 @@ if [ -e "bin/cli" ]; then
2225
fi
2326
# this converts the export map paths for the dist directory
2427
# and does a few other minor things
25-
node scripts/make-dist-package-json.cjs > dist/package.json
28+
node scripts/utils/make-dist-package-json.cjs > dist/package.json
2629

2730
# build to .js/.mjs/.d.ts files
2831
npm exec tsc-multi
@@ -32,22 +35,22 @@ cp src/_shims/auto/*.{d.ts,js,mjs} dist/_shims/auto
3235
# we need to add exports = module.exports = OpenAI Node to index.js;
3336
# No way to get that from index.ts because it would cause compile errors
3437
# when building .mjs
35-
node scripts/fix-index-exports.cjs
38+
node scripts/utils/fix-index-exports.cjs
3639
# with "moduleResolution": "nodenext", if ESM resolves to index.d.ts,
3740
# it'll have TS errors on the default import. But if it resolves to
3841
# index.d.mts the default import will work (even though both files have
3942
# the same export default statement)
4043
cp dist/index.d.ts dist/index.d.mts
4144
cp tsconfig.dist-src.json dist/src/tsconfig.json
4245

43-
node scripts/postprocess-files.cjs
46+
node scripts/utils/postprocess-files.cjs
4447

4548
# make sure that nothing crashes when we require the output CJS or
4649
# import the output ESM
4750
(cd dist && node -e 'require("openai")')
4851
(cd dist && node -e 'import("openai")' --input-type=module)
4952

50-
if command -v deno &> /dev/null && [ -e ./build-deno ]
53+
if command -v deno &> /dev/null && [ -e ./scripts/build-deno ]
5154
then
52-
./build-deno
55+
./scripts/build-deno
5356
fi

build-deno scripts/build-deno

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
set -exuo pipefail
44

5+
cd "$(dirname "$0")/.."
6+
57
rm -rf deno; mkdir deno
68
cp -rp src/* deno
79

@@ -37,7 +39,7 @@ done
3739
for file in LICENSE CHANGELOG.md; do
3840
if [ -e "${file}" ]; then cp "${file}" deno; fi
3941
done
40-
npm exec ts-node -T -- scripts/denoify.ts
42+
npm exec ts-node -T -- scripts/utils/denoify.ts
4143
deno fmt deno
4244
deno check deno/mod.ts
4345
if [ -e deno_tests ]; then

scripts/git-publish-deno.sh

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env bash
2+
23
set -exuo pipefail
34

5+
cd "$(dirname "$0")/.."
6+
47
# This script pushes the contents of the `deno` directory to the `deno` branch,
58
# and creates a `vx.x.x-deno` tag, so that Deno users can
69
# import OpenAI from "https://raw.githubusercontent.com/openai/openai-node/vx.x.x-deno/mod.ts"
@@ -38,7 +41,7 @@ else
3841
: "${DENO_PUSH_RELEASE_TAG:="v$DENO_PUSH_VERSION"}"
3942
fi
4043

41-
if [ ! -e deno ]; then ./build; fi
44+
if [ ! -e deno ]; then ./scripts/build; fi
4245

4346
# We want to commit and push a branch where everything inside the deno
4447
# directory is at root level in the branch.

scripts/lint

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
cd "$(dirname "$0")/.."
6+
7+
./node_modules/.bin/eslint --ext ts,js .

scripts/mock

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/usr/bin/env bash
22

3-
if [ -z "$1" ]; then
3+
set -e
4+
5+
cd "$(dirname "$0")/.."
6+
7+
if [ -n "$1" ]; then
48
URL="$1"
59
shift
610
else
@@ -15,7 +19,7 @@ fi
1519

1620
# Run prism mock on the given spec
1721
if [ "$1" == "--daemon" ]; then
18-
npm exec prism mock "$URL" &> .prism.log &
22+
npm exec --package=@stoplight/prism-cli@~5.3.2 -- prism mock "$URL" &> .prism.log &
1923

2024
# Wait for server to come online
2125
while ! grep -q "✖ fatal\|Prism is listening" ".prism.log" ; do
@@ -30,5 +34,5 @@ if [ "$1" == "--daemon" ]; then
3034

3135
echo
3236
else
33-
npm exec prism mock "$URL"
37+
npm exec --package=@stoplight/prism-cli@~5.3.2 -- prism mock "$URL"
3438
fi

scripts/test

+30-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
#!/usr/bin/env bash
22

3+
set -e
4+
5+
cd "$(dirname "$0")/.."
6+
7+
RED='\033[0;31m'
8+
GREEN='\033[0;32m'
9+
YELLOW='\033[0;33m'
10+
NC='\033[0m' # No Color
11+
312
function prism_is_running() {
413
curl --silent "http://localhost:4010" >/dev/null 2>&1
514
}
@@ -12,17 +21,32 @@ kill_server_on_port() {
1221
fi
1322
}
1423

15-
if ! prism_is_running; then
24+
function is_overriding_api_base_url() {
25+
[ -n "$TEST_API_BASE_URL" ]
26+
}
27+
28+
if ! is_overriding_api_base_url && ! prism_is_running ; then
1629
# When we exit this script, make sure to kill the background mock server process
1730
trap 'kill_server_on_port 4010' EXIT
1831

1932
# Start the dev server
20-
./scripts/mock --daemon
33+
./scripts/mock --daemon &> /dev/null
34+
fi
2135

22-
# Sanity check and print a nice error message
23-
if ! ./bin/check-test-server; then
24-
exit
25-
fi
36+
if ! prism_is_running ; then
37+
echo -e "${RED}ERROR:${NC} The test suite will not run without a mock Prism server"
38+
echo -e "running against your OpenAPI spec."
39+
echo
40+
echo -e "To run the server, pass in the path or url of your OpenAPI"
41+
echo -e "spec to the prism command:"
42+
echo
43+
echo -e " \$ ${YELLOW}npm exec prism mock path/to/your.openapi.yml${NC}"
44+
echo
45+
46+
exit 1
47+
else
48+
echo -e "${GREEN}✔ Mock prism server is running with your OpenAPI spec${NC}"
49+
echo
2650
fi
2751

2852
# Run tests
File renamed without changes.

scripts/check-version.cjs scripts/utils/check-version.cjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ const fs = require('fs');
22
const path = require('path');
33

44
const main = () => {
5-
const pkg = require('../package.json');
5+
const pkg = require('../../package.json');
66
const version = pkg['version'];
77
if (!version) throw 'The version property is not set in the package.json file';
88
if (typeof version !== 'string') {
99
throw `Unexpected type for the package.json version field; got ${typeof version}, expected string`;
1010
}
1111

12-
const versionFile = path.resolve(__dirname, '..', 'src', 'version.ts');
12+
const versionFile = path.resolve(__dirname, '..', '..', 'src', 'version.ts');
1313
const contents = fs.readFileSync(versionFile, 'utf8');
1414
const output = contents.replace(/(export const VERSION = ')(.*)(')/g, `$1${version}$3`);
1515
fs.writeFileSync(versionFile, output);

scripts/denoify.ts scripts/utils/denoify.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import path from 'path';
22
import * as tm from 'ts-morph';
3-
import { name as pkgName } from '../package.json';
3+
import { name as pkgName } from '../../package.json';
44
import fs from 'fs';
55

6-
const rootDir = path.resolve(__dirname, '..');
6+
const rootDir = path.resolve(__dirname, '../..');
77
const denoDir = path.join(rootDir, 'deno');
88
const tsConfigFilePath = path.join(rootDir, 'tsconfig.deno.json');
99

scripts/fix-index-exports.cjs scripts/utils/fix-index-exports.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const path = require('path');
44
const indexJs =
55
process.env['DIST_PATH'] ?
66
path.resolve(process.env['DIST_PATH'], 'index.js')
7-
: path.resolve(__dirname, '..', 'dist', 'index.js');
7+
: path.resolve(__dirname, '..', '..', 'dist', 'index.js');
88

99
let before = fs.readFileSync(indexJs, 'utf8');
1010
let after = before.replace(

scripts/make-dist-package-json.cjs scripts/utils/make-dist-package-json.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const pkgJson = require(process.env['PKG_JSON_PATH'] || '../package.json');
1+
const pkgJson = require(process.env['PKG_JSON_PATH'] || '../../package.json');
22

33
function processExportMap(m) {
44
for (const key in m) {

scripts/postprocess-files.cjs scripts/utils/postprocess-files.cjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ const fs = require('fs');
22
const path = require('path');
33
const { parse } = require('@typescript-eslint/parser');
44

5-
const pkgImportPath = process.env['PKG_IMPORT_PATH'] ?? 'openai/'
5+
const pkgImportPath = process.env['PKG_IMPORT_PATH'] ?? 'openai/';
66

77
const distDir =
88
process.env['DIST_PATH'] ?
99
path.resolve(process.env['DIST_PATH'])
10-
: path.resolve(__dirname, '..', 'dist');
10+
: path.resolve(__dirname, '..', '..', 'dist');
1111
const distSrcDir = path.join(distDir, 'src');
1212

1313
/**
@@ -103,7 +103,7 @@ async function* walk(dir) {
103103
}
104104

105105
async function postprocess() {
106-
for await (const file of walk(path.resolve(__dirname, '..', 'dist'))) {
106+
for await (const file of walk(path.resolve(__dirname, '..', '..', 'dist'))) {
107107
if (!/\.([cm]?js|(\.d)?[cm]?ts)$/.test(file)) continue;
108108

109109
const code = await fs.promises.readFile(file, 'utf8');

0 commit comments

Comments
 (0)