-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-packaging.sh
300 lines (245 loc) · 10.1 KB
/
test-packaging.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env zsh
# Load some utilities
die() {
local message="$*"
>&2 echo "${message}"
exit 1
}
read -r -d '' USAGE << EOM
NAME
package-and-install – package a gatsby theme and build a new site using the package
SYNOPSIS
package-and-install [-t templateDir] [-r registryPackage1,2,...] [-w workspacePackage1,2,...] \
[-a artifactDir] [-i {yarn,npm}] [-c develop,build,serve] [-h]
DESCRIPTION
The package-and-install tool:
0) initializes a Gatsby site from empty or based on a template,
1) creates new packages based on a Gatsby theme and plugins in the current workspace,
2) adds them as a dependency to a Gatsby site,
3) builds the site.
The options are as follows:
-t templateDir
A template directory to duplicate for the Gatsby site. If unset, an empty site is created.
-r registryPackages
The names of packages to install from the registry, separated by commas.
They should be identical to the "name" parameter in the plugins' package.json files,
plus optionally a version number.
If unset, no additional packages are added from the registry.
Examples:
-r "gatsby,react,react-dom"
-r "gatsby@^4.24.0,[email protected],[email protected]"
-r "gatsby@^4.24.0,[email protected],[email protected],@thepolicylab-projectportals/gatsby-theme-project-portal,@thepolicylab-projectportals/project-portal-content-decap"
-w workspacePackages
The names of packages to create and install from workspaces, separated by commas.
They should be identical to the "name" parameter in the plugins' package.json files.
If unset, no additional packages are added from the workspaces.
Examples:
-w "@thepolicylab-projectportals/gatsby-theme-project-portal,@thepolicylab-projectportals/project-portal-content-decap"
-w "@thepolicylab-projectportals/project-portal-content-airtable"
-w ""
-a artifactDir
The location where the packed theme is stored.
Default: ./artifacts
-i {yarn,npm}
The package manager to use for the installation in the target directory.
Default: yarn
-c gatsbyCommands
The commands to execute on the site directory after creation, separated by commas.
Default: build
Examples:
-c develop
-c build,serve
-h
Show this help message and exit
EXAMPLES
Create an empty site using the current local version (minimum example):
% package-and-install -r react@^18.0.0,react-dom@^18.0.0,gatsby@^5.0.0 -w @thepolicylab-projectportals/gatsby-theme-project-portal -g @thepolicylab-projectportals/gatsby-theme-project-portal
Create a new duplicate of the "defaults" site:
% package-and-install -t "packages/defaults/" -w @thepolicylab-projectportals/gatsby-theme-project-portal
Create a new duplicate of the "defaults" site, using npm to install the node_modules:
% package-and-install -i npm -t "packages/defaults/" -w @thepolicylab-projectportals/gatsby-theme-project-portal
Create a new duplicate of the "example" site:
% package-and-install -t "packages/example/" -w @thepolicylab-projectportals/gatsby-theme-project-portal
Create a new duplicate of the "example-content" site:
% package-and-install -t "packages/example-site/" -w @thepolicylab-projectportals/gatsby-theme-project-portal,@thepolicylab-projectportals/project-portal-content-decap
EOM
read -r -d '' YARNRC << EOM
nodeLinker: node-modules
EOM
# Specify the template site
package-and-install () {
setopt LOCAL_OPTIONS xtrace
# Specify any packages we need to install from registries
registryPackages=""
# Specify any packages we need to install from workspaces
workspacePackages=""
# Specify any packages we need to include in gatsby config
gatsbyConfigPackages=""
# Specify the templateDir (must be a gatsby site)
# Examples:
# packages/example/
# packages/defaults/
templateDir=""
# Specify how the site will be created. This is implicitly set if a template is defined.
# empty (default) – create an empty site and add the minimum code using the package manager
# template – create a site based on an existing directory
initMethod="empty"
# Specify where the pack file is stored
artifactDir="$(pwd)/artifacts"
# Specify which package manager to use
packageManager="yarn"
# Specify which commands to execute after creation
gatsbyCommands="build"
while getopts t:r:w:g:a:i:c:h flag
do
case "${flag}" in
t) {
templateDir=${OPTARG}
initMethod="template"
};;
r) registryPackages=${OPTARG};;
w) workspacePackages=${OPTARG};;
g) gatsbyConfigPackages=${OPTARG};;
a) artifactDir=${OPTARG};;
i) packageManager=${OPTARG};;
c) gatsbyCommands=${OPTARG};;
h) echo "$USAGE" | more; return 1;;
*) echo "flag ${flag} not recognized" ; return 1
esac
done
echo "Running package-and-install with: "
echo "init method ... template directory: ${initMethod} ... '${templateDir}'"
echo "registryPackages: ${registryPackages}"
echo "workspacePackages: ${workspacePackages}"
echo "gatsbyConfigPackages: ${gatsbyConfigPackages}"
echo "artifactDir: ${artifactDir}"
echo "gatsbyCommands:" ${gatsbyCommands}
###################
# Local Preparation
###################
# Create all the packages locally we'll need later and add them to arrays
# Define a variable to hold all of the packages we need to install
declare -a packageManagerAddList
# List any packages we need from the registries
registryPackagesArray=(${(s/,/)registryPackages})
echo "registry packages"
for registryPackage in "${registryPackagesArray[@]}"
do
echo "including ${registryPackage}"
packageManagerAddList+=($registryPackage)
done
# List any packages we need from the workspaces
workspacePackagesArray=(${(s/,/)workspacePackages})
for workspacePackage in "${workspacePackagesArray[@]}"
do
echo "packaging ${workspacePackage}"
packagePrefix=$(basename "$workspacePackage")
packPath="$artifactDir/$packagePrefix-$(date '+%s')-$(git rev-parse --short HEAD)-$(base64 < "/dev/urandom" | tr -dc '0-9a-zA-Z' | head -c3 ).tgz"
mkdir -p "$artifactDir"
yarn workspace "$workspacePackage" pack --out "$packPath"
echo "including $packPath"
case "${packageManager}" in
yarn) {
packageManagerAddList+=("$workspacePackage@file:${packPath}")
} ;;
npm) {
packageManagerAddList+=($packPath)
} ;;
*) die "package manager ${packageManager} unknown, can't add.";;
esac
done
###############
# TestDir Setup
###############
# Create an empty directory testDir where we can test the installation
testDir=$(mktemp -d || die "Failed to create new temporary directory.")
echo "new temporary directory: $testDir"
(
cd "$testDir" || die "Failed to cd to testDir '$testDir'"
# Set up the package manager
case "${packageManager}" in
yarn) {
echo "$YARNRC" > "$testDir/.yarnrc.yml" # initialize the yarnrc first
yarn set version berry # ... then set the version, as this modifies the yarnrc
};;
npm) {
echo
};;
*) die "package manager ${packageManager} unknown, can't serve.";;
esac
echo "using ${packageManager} version $(${packageManager} -v)"
)
# Create the empty or template Gatsby site
case "${initMethod}" in
empty) {
(
cd "$testDir" || die "Failed to cd to testDir '$testDir'"
${packageManager} init -y || die "Failed to init new site"
# Add anything we need to Gatsby Config
plugins=""
gatsbyConfigPackagesArray=(${(s/,/)gatsbyConfigPackages})
for gatsbyConfigPackage in "${gatsbyConfigPackagesArray[@]}"
do
plugins="${plugins} \`${gatsbyConfigPackage}\`,"
done
echo "module.exports = { plugins: [${plugins}] }" > "gatsby-config.js"
)
};;
template) {
# Sync content from the template site to the testDir
rsync -av --progress "$templateDir/." "$testDir" --exclude node_modules --exclude .cache --exclude public
};;
esac
# Add everything we need in one go
(
if [ ${#packageManagerAddList[@]} -eq 0 ]; then
echo "no packages to add."
else
echo "installing all of " "${packageManagerAddList[@]}"
cd "$testDir" || die "Failed to cd to testDir '$testDir'"
${packageManager} add "${packageManagerAddList[@]}" || die "Failed to add dependencies'"
fi
)
(
# Navigate to the test directory
cd "$testDir" || die "couldn't cd to testDir '$testDir'"
# Show the current version of the package.json
cat package.json
${packageManager} install || die "failed to install all dependencies"
gatsbyCommandsArray=(${(s/,/)gatsbyCommands})
for gatsbyCommand in "${gatsbyCommandsArray[@]}"
do
case "${packageManager}" in
yarn) {
# Run the command
yarn gatsby "$gatsbyCommand" || die "failed to execute 'gatsby $gatsbyCommand'"
} ;;
npm) {
# Run the command
npm run env -- gatsby "$gatsbyCommand" || die "failed to execute 'gatsby $gatsbyCommand'"
} ;;
*) die "package manager ${packageManager} unknown, can't run commands";;
esac
done
# Tell the user how to interact with the site
case "${packageManager}" in
yarn) {
echo "To serve the built site run:"
echo "(cd $testDir && yarn gatsby serve)"
echo "To start a clean dev server run:"
echo "(cd $testDir && yarn gatsby clean && yarn gatsby develop)"
echo "To rebuild and serve, run:"
echo "(cd $testDir && yarn gatsby clean && yarn gatsby build && yarn gatsby serve)"
};;
npm) {
echo "To serve the built site run:"
echo "(cd $testDir && npm run env -- gatsby develop)"
echo "To start dev server run:"
echo "(cd $testDir && npm run env -- gatsby clean && npm run env -- gatsby develop)"
echo "To rebuild and serve, run:"
echo "(cd $testDir && npm run env -- gatsby clean && npm run env -- gatsby build && npm run env -- gatsby serve)"
};;
*) die "package manager ${packageManager} unknown, can't serve.";;
esac
)
}