From 6c0fb07f80cae8c1a3444c0b12e6a322abeda0a7 Mon Sep 17 00:00:00 2001 From: William French Date: Thu, 16 Jul 2026 09:49:22 -0700 Subject: [PATCH 1/2] Update migration and refactoring scripts --- samples/migrate-sample.sh | 15 +++++++-------- samples/new-sample.sh | 36 +++++++++++++++++------------------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/samples/migrate-sample.sh b/samples/migrate-sample.sh index 369b73857..8c56d87c3 100755 --- a/samples/migrate-sample.sh +++ b/samples/migrate-sample.sh @@ -38,7 +38,7 @@ cp "${INPUT_DIR}/dist/samples/${NAME}/docs/style.css" "${OUTPUT_DIR}/${NAME}/" # Generate a placeholder for index.js. touch "${OUTPUT_DIR}/${NAME}/index.js" cat > "${OUTPUT_DIR}/${NAME}/index.js" << EOF - +// Placeholder for index.js (run TSC to generate). EOF # Generate package.json @@ -50,10 +50,11 @@ cat > "${OUTPUT_DIR}/${NAME}/package.json" << EOF "scripts": { "build": "bash ../build-single.sh", "test": "tsc && npm run build:vite --workspace=.", - "start": "tsc && vite build --base './' && vite", - "build:vite": "vite build --base './'", - "preview": "vite preview" - } + "start": "tsc && vite build --config ../../vite.config.js --base './' && vite --config ../../vite.config.js", + "build:vite": "vite build --config ../../vite.config.js --base './'", + "preview": "vite preview --config ../../vite.config.js" + }, + "author": "Google LLC" } EOF @@ -65,9 +66,7 @@ cat > "${OUTPUT_DIR}/${NAME}/tsconfig.json" << EOF "compilerOptions": { "rootDir": "." }, - "include": [ - "./*.ts" - ] + "include": ["./*.ts"] } EOF diff --git a/samples/new-sample.sh b/samples/new-sample.sh index e68484ad4..ef214c63d 100755 --- a/samples/new-sample.sh +++ b/samples/new-sample.sh @@ -50,15 +50,14 @@ cat >> "$NAME/index.html" << 'EOF' EOF cat >> "$NAME/index.html" << EOF - - + EOF @@ -74,9 +73,9 @@ cat > "$NAME/style.css" << EOF /* [START $REGION_TAG] */ html, body { - height: 100%; - margin: 0; - padding: 0; + height: 100%; + margin: 0; + padding: 0; } /* [END $REGION_TAG] */ EOF @@ -84,17 +83,18 @@ EOF # Create package.json cat > "$NAME/package.json" << EOF { - "name": "@js-api-samples/$NAME", - "version": "1.0.0", - "scripts": { - "build": "bash ../build-single.sh", - "test": "tsc && npm run build:vite --workspace=.", - "start": "tsc && vite build --base './' && vite", - "build:vite": "vite build --base './'", - "preview": "vite preview" - } + "name": "@js-api-samples/$NAME", + "version": "1.0.0", + "scripts": { + "build": "bash ../build-single.sh", + "test": "tsc && npm run build:vite --workspace=.", + "start": "tsc && vite build --config ../../vite.config.js --base './' && vite --config ../../vite.config.js", + "build:vite": "vite build --config ../../vite.config.js --base './'", + "preview": "vite preview --config ../../vite.config.js" + }, + "author": "Google LLC" } -EOF +EOF # Create tsconfig.json cat > "$NAME/tsconfig.json" << 'EOF' @@ -103,9 +103,7 @@ cat > "$NAME/tsconfig.json" << 'EOF' "compilerOptions": { "rootDir": "." }, - "include": [ - "./*.ts" - ] + "include": ["./*.ts"] } EOF From ac250bd9150c9535d8d2a287efeda2a66e93de1b Mon Sep 17 00:00:00 2001 From: William French Date: Thu, 16 Jul 2026 12:23:47 -0700 Subject: [PATCH 2/2] Extract shared configuration generation into generate-shared-boilerplate.sh to DRY up sample generation scripts --- samples/generate-shared-boilerplate.sh | 88 ++++++++++++++++++++++++++ samples/migrate-sample.sh | 76 +--------------------- samples/new-sample.sh | 74 +--------------------- 3 files changed, 93 insertions(+), 145 deletions(-) create mode 100755 samples/generate-shared-boilerplate.sh diff --git a/samples/generate-shared-boilerplate.sh b/samples/generate-shared-boilerplate.sh new file mode 100755 index 000000000..1ac143ab8 --- /dev/null +++ b/samples/generate-shared-boilerplate.sh @@ -0,0 +1,88 @@ +#!/bin/bash + +# A helper script to generate common boilerplate files (package.json, tsconfig.json, README.md) +# for Google Maps JavaScript samples. +# Usage: ./generate-shared-boilerplate.sh <OUTPUT_DIR> + +NAME="$1" +TITLE="$2" +OUTPUT_DIR="${3:-.}" + +if [ -z "$NAME" ] || [ -z "$TITLE" ]; then + echo "Usage: $0 <NAME> <TITLE> [OUTPUT_DIR]" + exit 1 +fi + +mkdir -p "${OUTPUT_DIR}/${NAME}" + +# Generate package.json +cat > "${OUTPUT_DIR}/${NAME}/package.json" << EOF +{ + "name": "@js-api-samples/${NAME}", + "version": "1.0.0", + "scripts": { + "build": "bash ../build-single.sh", + "test": "tsc && npm run build:vite --workspace=.", + "start": "tsc && vite build --config ../../vite.config.js --base './' && vite --config ../../vite.config.js", + "build:vite": "vite build --config ../../vite.config.js --base './'", + "preview": "vite preview --config ../../vite.config.js" + }, + "author": "Google LLC" +} +EOF + +# Generate tsconfig.json +cat > "${OUTPUT_DIR}/${NAME}/tsconfig.json" << 'EOF' +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "rootDir": "." + }, + "include": ["./*.ts"] +} +EOF + +# Generate README.md +cat > "${OUTPUT_DIR}/${NAME}/README.md" << EOF +# Google Maps JavaScript Sample + +## ${NAME} + +${TITLE} + +## Setup + +### Before starting run: + +\`npm i\` + +### Run an example on a local web server + +\`cd samples/${NAME}\` +\`npm start\` + +### Build an individual example + +\`cd samples/${NAME}\` +\`npm run build\` + +From 'samples': + +\`npm run build --workspace=${NAME}/\` + +### Build all of the examples. + +From 'samples': + +\`npm run build-all\` + +### Run lint to check for problems + +\`cd samples/${NAME}\` +\`npx eslint index.ts\` + +## Feedback + +For feedback related to this sample, please open a new issue on +[GitHub](https://github.com/googlemaps-samples/js-api-samples/issues). +EOF diff --git a/samples/migrate-sample.sh b/samples/migrate-sample.sh index 8c56d87c3..dab096b47 100755 --- a/samples/migrate-sample.sh +++ b/samples/migrate-sample.sh @@ -41,80 +41,8 @@ cat > "${OUTPUT_DIR}/${NAME}/index.js" << EOF // Placeholder for index.js (run TSC to generate). EOF -# Generate package.json -touch "${OUTPUT_DIR}/${NAME}/package.json" -cat > "${OUTPUT_DIR}/${NAME}/package.json" << EOF -{ - "name": "@js-api-samples/${NAME}", - "version": "1.0.0", - "scripts": { - "build": "bash ../build-single.sh", - "test": "tsc && npm run build:vite --workspace=.", - "start": "tsc && vite build --config ../../vite.config.js --base './' && vite --config ../../vite.config.js", - "build:vite": "vite build --config ../../vite.config.js --base './'", - "preview": "vite preview --config ../../vite.config.js" - }, - "author": "Google LLC" -} -EOF - -# Generate tsconfig.json -touch "${OUTPUT_DIR}/${NAME}/tsconfig.json" -cat > "${OUTPUT_DIR}/${NAME}/tsconfig.json" << EOF -{ - "extends": "../../tsconfig.base.json", - "compilerOptions": { - "rootDir": "." - }, - "include": ["./*.ts"] -} -EOF - -# Generate README.md -touch "${OUTPUT_DIR}/${NAME}/README.md" -cat > "${OUTPUT_DIR}/${NAME}/README.md" << EOF -# Google Maps JavaScript Sample - -## ${NAME} - -${TITLE} - -## Setup - -### Before starting run: - -\`npm i\` - -### Run an example on a local web server - -\`cd samples/${NAME}\` -\`npm start\` - -### Build an individual example - -\`cd samples/${NAME}\` -\`npm run build\` - -From 'samples': - -\`npm run build --workspace=${NAME}/\` - -### Build all of the examples. - -From 'samples': - -\`npm run build-all\` - -### Run lint to check for problems - -\`cd samples/${NAME}\` -\`npx eslint index.ts\` - -## Feedback - -For feedback related to this sample, please open a new issue on -[GitHub](https://github.com/googlemaps-samples/js-api-samples/issues). -EOF +# Generate shared boilerplate +bash "$(dirname "$0")/generate-shared-boilerplate.sh" "$NAME" "$TITLE" "$OUTPUT_DIR" # Git add the files. git add "${OUTPUT_DIR}/${NAME}/index.ts" diff --git a/samples/new-sample.sh b/samples/new-sample.sh index ef214c63d..b15eed5b6 100755 --- a/samples/new-sample.sh +++ b/samples/new-sample.sh @@ -80,76 +80,8 @@ body { /* [END $REGION_TAG] */ EOF -# Create package.json -cat > "$NAME/package.json" << EOF -{ - "name": "@js-api-samples/$NAME", - "version": "1.0.0", - "scripts": { - "build": "bash ../build-single.sh", - "test": "tsc && npm run build:vite --workspace=.", - "start": "tsc && vite build --config ../../vite.config.js --base './' && vite --config ../../vite.config.js", - "build:vite": "vite build --config ../../vite.config.js --base './'", - "preview": "vite preview --config ../../vite.config.js" - }, - "author": "Google LLC" -} -EOF - -# Create tsconfig.json -cat > "$NAME/tsconfig.json" << 'EOF' -{ - "extends": "../../tsconfig.base.json", - "compilerOptions": { - "rootDir": "." - }, - "include": ["./*.ts"] -} -EOF - -# Create README.md -cat > "$NAME/README.md" << EOF -# Google Maps JavaScript Sample - -## $NAME - -Add a meaningful description for $NAME here... - -## Setup - -### Before starting run: - -\`npm i\` - -### Run an example on a local web server - -\`cd samples/$NAME\` -\`npm start\` - -### Build an individual example - -\`cd samples/$NAME\` -\`npm run build\` - -From 'samples': - -\`npm run build --workspace=$NAME/\` - -### Build all of the examples. - -From 'samples': - -\`npm run build-all\` - -### Run lint to check for problems - -\`cd samples/$NAME\` -\`npx eslint index.ts\` - -## Feedback - -For feedback related to this sample, please open a new issue on -[GitHub](https://github.com/googlemaps-samples/js-api-samples/issues). -EOF +# Generate shared boilerplate +TITLE="Add a meaningful description for $NAME here..." +bash "$(dirname "$0")/generate-shared-boilerplate.sh" "$NAME" "$TITLE" "." echo "Sample $NAME created successfully in directory ./$NAME"