Skip to content

Commit

Permalink
chore(internal): refactor scripts (#806)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored Apr 30, 2024
1 parent 3dabda8 commit 9283519
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 80 deletions.
50 changes: 0 additions & 50 deletions bin/check-test-server

This file was deleted.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
"private": false,
"scripts": {
"test": "./scripts/test",
"build": "bash ./build",
"build": "./scripts/build",
"prepack": "echo 'to pack, run yarn build && (cd dist; yarn pack)' && exit 1",
"prepublishOnly": "echo 'to publish, run yarn build && (cd dist; yarn publish)' && exit 1",
"format": "prettier --write --cache --cache-strategy metadata . !dist",
"prepare": "if ./scripts/check-is-in-git-install.sh; then npm run build; fi",
"prepare": "if ./scripts/utils/check-is-in-git-install.sh; then ./scripts/build; fi",
"tsn": "ts-node -r tsconfig-paths/register",
"lint": "eslint --ext ts,js .",
"lint": "./scripts/lint",
"fix": "eslint --fix --ext ts,js ."
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@
"extra-files": [
"src/version.ts",
"README.md",
"build-deno"
"./scripts/build-deno"
]
}
9 changes: 9 additions & 0 deletions scripts/bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -e

cd "$(dirname "$0")/.."

PACKAGE_MANAGER=$(command -v yarn >/dev/null 2>&1 && echo "yarn" || echo "npm")

$PACKAGE_MANAGER install
15 changes: 9 additions & 6 deletions build → scripts/build
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#!/usr/bin/env bash

set -exuo pipefail

node scripts/check-version.cjs
cd "$(dirname "$0")/.."

node scripts/utils/check-version.cjs

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

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

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

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

if command -v deno &> /dev/null && [ -e ./build-deno ]
if command -v deno &> /dev/null && [ -e ./scripts/build-deno ]
then
./build-deno
./scripts/build-deno
fi
4 changes: 3 additions & 1 deletion build-deno → scripts/build-deno
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

set -exuo pipefail

cd "$(dirname "$0")/.."

rm -rf deno; mkdir deno
cp -rp src/* deno

Expand Down Expand Up @@ -37,7 +39,7 @@ done
for file in LICENSE CHANGELOG.md; do
if [ -e "${file}" ]; then cp "${file}" deno; fi
done
npm exec ts-node -T -- scripts/denoify.ts
npm exec ts-node -T -- scripts/utils/denoify.ts
deno fmt deno
deno check deno/mod.ts
if [ -e deno_tests ]; then
Expand Down
5 changes: 4 additions & 1 deletion scripts/git-publish-deno.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env bash

set -exuo pipefail

cd "$(dirname "$0")/.."

# This script pushes the contents of the `deno` directory to the `deno` branch,
# and creates a `vx.x.x-deno` tag, so that Deno users can
# import OpenAI from "https://raw.githubusercontent.com/openai/openai-node/vx.x.x-deno/mod.ts"
Expand Down Expand Up @@ -38,7 +41,7 @@ else
: "${DENO_PUSH_RELEASE_TAG:="v$DENO_PUSH_VERSION"}"
fi

if [ ! -e deno ]; then ./build; fi
if [ ! -e deno ]; then ./scripts/build; fi

# We want to commit and push a branch where everything inside the deno
# directory is at root level in the branch.
Expand Down
7 changes: 7 additions & 0 deletions scripts/lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -e

cd "$(dirname "$0")/.."

./node_modules/.bin/eslint --ext ts,js .
10 changes: 7 additions & 3 deletions scripts/mock
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/usr/bin/env bash

if [ -z "$1" ]; then
set -e

cd "$(dirname "$0")/.."

if [ -n "$1" ]; then
URL="$1"
shift
else
Expand All @@ -15,7 +19,7 @@ fi

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

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

echo
else
npm exec prism mock "$URL"
npm exec --package=@stoplight/prism-cli@~5.3.2 -- prism mock "$URL"
fi
36 changes: 30 additions & 6 deletions scripts/test
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#!/usr/bin/env bash

set -e

cd "$(dirname "$0")/.."

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

function prism_is_running() {
curl --silent "http://localhost:4010" >/dev/null 2>&1
}
Expand All @@ -12,17 +21,32 @@ kill_server_on_port() {
fi
}

if ! prism_is_running; then
function is_overriding_api_base_url() {
[ -n "$TEST_API_BASE_URL" ]
}

if ! is_overriding_api_base_url && ! prism_is_running ; then
# When we exit this script, make sure to kill the background mock server process
trap 'kill_server_on_port 4010' EXIT

# Start the dev server
./scripts/mock --daemon
./scripts/mock --daemon &> /dev/null
fi

# Sanity check and print a nice error message
if ! ./bin/check-test-server; then
exit
fi
if ! prism_is_running ; then
echo -e "${RED}ERROR:${NC} The test suite will not run without a mock Prism server"
echo -e "running against your OpenAPI spec."
echo
echo -e "To run the server, pass in the path or url of your OpenAPI"
echo -e "spec to the prism command:"
echo
echo -e " \$ ${YELLOW}npm exec prism mock path/to/your.openapi.yml${NC}"
echo

exit 1
else
echo -e "${GREEN}✔ Mock prism server is running with your OpenAPI spec${NC}"
echo
fi

# Run tests
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions scripts/check-version.cjs → scripts/utils/check-version.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ const fs = require('fs');
const path = require('path');

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

const versionFile = path.resolve(__dirname, '..', 'src', 'version.ts');
const versionFile = path.resolve(__dirname, '..', '..', 'src', 'version.ts');
const contents = fs.readFileSync(versionFile, 'utf8');
const output = contents.replace(/(export const VERSION = ')(.*)(')/g, `$1${version}$3`);
fs.writeFileSync(versionFile, output);
Expand Down
4 changes: 2 additions & 2 deletions scripts/denoify.ts → scripts/utils/denoify.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from 'path';
import * as tm from 'ts-morph';
import { name as pkgName } from '../package.json';
import { name as pkgName } from '../../package.json';
import fs from 'fs';

const rootDir = path.resolve(__dirname, '..');
const rootDir = path.resolve(__dirname, '../..');
const denoDir = path.join(rootDir, 'deno');
const tsConfigFilePath = path.join(rootDir, 'tsconfig.deno.json');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const path = require('path');
const indexJs =
process.env['DIST_PATH'] ?
path.resolve(process.env['DIST_PATH'], 'index.js')
: path.resolve(__dirname, '..', 'dist', 'index.js');
: path.resolve(__dirname, '..', '..', 'dist', 'index.js');

let before = fs.readFileSync(indexJs, 'utf8');
let after = before.replace(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const pkgJson = require(process.env['PKG_JSON_PATH'] || '../package.json');
const pkgJson = require(process.env['PKG_JSON_PATH'] || '../../package.json');

function processExportMap(m) {
for (const key in m) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ const fs = require('fs');
const path = require('path');
const { parse } = require('@typescript-eslint/parser');

const pkgImportPath = process.env['PKG_IMPORT_PATH'] ?? 'openai/'
const pkgImportPath = process.env['PKG_IMPORT_PATH'] ?? 'openai/';

const distDir =
process.env['DIST_PATH'] ?
path.resolve(process.env['DIST_PATH'])
: path.resolve(__dirname, '..', 'dist');
: path.resolve(__dirname, '..', '..', 'dist');
const distSrcDir = path.join(distDir, 'src');

/**
Expand Down Expand Up @@ -103,7 +103,7 @@ async function* walk(dir) {
}

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

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

0 comments on commit 9283519

Please sign in to comment.