Skip to content

Commit

Permalink
chore(internal): refactor scripts (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Apr 30, 2024
1 parent c2da960 commit f60e2d8
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 81 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
6 changes: 3 additions & 3 deletions packages/bedrock-sdk/build
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ done

# this converts the export map paths for the dist directory
# and does a few other minor things
PKG_JSON_PATH=../packages/bedrock-sdk/package.json node ../../scripts/make-dist-package-json.cjs > dist/package.json
PKG_JSON_PATH=../../packages/bedrock-sdk/package.json node ../../scripts/utils/make-dist-package-json.cjs > dist/package.json

# updates the `@anthropic-ai/sdk` dependency to point to NPM
node scripts/postprocess-dist-package-json.cjs
Expand All @@ -27,15 +27,15 @@ npm exec tsc-multi
# we need to add exports = module.exports = Anthropic TypeScript to index.js;
# No way to get that from index.ts because it would cause compile errors
# when building .mjs
DIST_PATH=./dist node ../../scripts/fix-index-exports.cjs
DIST_PATH=./dist 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

DIST_PATH=./dist PKG_IMPORT_PATH=@anthropic-ai/bedrock-sdk/ node ../../scripts/postprocess-files.cjs
DIST_PATH=./dist PKG_IMPORT_PATH=@anthropic-ai/bedrock-sdk/ node ../../scripts/utils/postprocess-files.cjs

# make sure that nothing crashes when we require the output CJS or
# import the output ESM
Expand Down
6 changes: 3 additions & 3 deletions packages/vertex-sdk/build
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ done

# this converts the export map paths for the dist directory
# and does a few other minor things
PKG_JSON_PATH=../packages/vertex-sdk/package.json node ../../scripts/make-dist-package-json.cjs > dist/package.json
PKG_JSON_PATH=../../packages/vertex-sdk/package.json node ../../scripts/utils/make-dist-package-json.cjs > dist/package.json

# updates the `@anthropic-ai/sdk` dependency to point to NPM
node scripts/postprocess-dist-package-json.cjs
Expand All @@ -27,15 +27,15 @@ npm exec tsc-multi
# we need to add exports = module.exports = Anthropic TypeScript to index.js;
# No way to get that from index.ts because it would cause compile errors
# when building .mjs
DIST_PATH=./dist node ../../scripts/fix-index-exports.cjs
DIST_PATH=./dist 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

DIST_PATH=./dist PKG_IMPORT_PATH=@anthropic-ai/vertex-sdk/ node ../../scripts/postprocess-files.cjs
DIST_PATH=./dist PKG_IMPORT_PATH=@anthropic-ai/vertex-sdk/ node ../../scripts/utils/postprocess-files.cjs

# make sure that nothing crashes when we require the output CJS or
# import the output ESM
Expand Down
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 = Anthropic TypeScript 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("@anthropic-ai/sdk")')
(cd dist && node -e 'import("@anthropic-ai/sdk")' --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
0 build-all → scripts/build-all
100644 → 100755
File renamed without changes.
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
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'] ?? '@anthropic-ai/sdk/'
const pkgImportPath = process.env['PKG_IMPORT_PATH'] ?? '@anthropic-ai/sdk/';

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 f60e2d8

Please sign in to comment.