Browse Source

Add command to test the source code for release

Using `yarn test-release`, a series of commands will run to do a suite
of tests:
- Typescript check
- ESLint check
- Unit tests run
- Production build
- Production build start

The build will revert to development after the production build is run
successfully.
Sergiu Miclea 4 years ago
parent
commit
e9628bbe33
3 changed files with 65 additions and 1 deletions
  1. 1 1
      README.md
  2. 1 0
      package.json
  3. 63 0
      server/testRelease.js

+ 1 - 1
README.md

@@ -21,7 +21,7 @@ Your server will be running at `http://localhost:3000/` (the port is configurabl
 ## Testing
 
 - unit tests can be run using `yarn test`
-- e2e integration tests can be run using `yarn e2e`. First though, you have to create the `private/cypress/config.js` file using `private/cypress/config.template.js` as a template and then run `yarn build` and `node server`.
+- run `yarn test-release` to check for Typescript and ESLint errors, to run the unit tests and to build and start a production build.
 
 ## Development mode
 

+ 1 - 0
package.json

@@ -13,6 +13,7 @@
     "server-debug": "node --inspect server",
     "tsc": "npx tsc --skipLibCheck",
     "eslint": "npx eslint \"src/**\" \"server/**\"",
+    "test-release": "node ./server/testRelease",
     "storybook": "start-storybook"
   },
   "devDependencies": {

+ 63 - 0
server/testRelease.js

@@ -0,0 +1,63 @@
+const spawn = require('child_process').spawn
+
+const TOTAL_SPAWN_STEPS = 7
+
+const main = async () => {
+  let currentSpawnStep = 0
+
+  const logProgress = (message) => {
+    currentSpawnStep += 1
+    console.log(`\n\x1b[36m${currentSpawnStep}/${TOTAL_SPAWN_STEPS} ${message}...\x1b[0m\n`)
+  }
+
+  const spawnPromise = (command, args, message) => new Promise((resolve, reject) => {
+    logProgress(message)
+    const childProcess = spawn(command, args, { stdio: 'inherit' })
+    childProcess.on('close', (code) => {
+      if (code === 0) {
+        resolve()
+      } else {
+        reject(new Error(`${command} ${args.join(' ')} exited with code ${code}`))
+      }
+    })
+  })
+
+  const spawnYarnStart = async (message) => new Promise((resolve, reject) => {
+    logProgress(message)
+    process.env.FORCE_COLOR = true
+    const childProcess = spawn('yarn', ['start'], { env: process.env })
+    childProcess.stdout.on('data', data => {
+      process.stdout.write(data)
+      if (data.toString().includes('server is up')) {
+        console.log('\nContinue to next step? (Y/n)...')
+      }
+    })
+    childProcess.stderr.on('data', data => { process.stderr.write(data) })
+    process.stdin.setRawMode(true)
+    process.stdin.once('data', data => {
+      childProcess.kill()
+      process.stdin.setRawMode(false)
+      if (data.toString() === 'n') {
+        reject(new Error('Aborted'))
+      } else {
+        resolve()
+      }
+    })
+  })
+
+  try {
+    await spawnPromise('yarn', ['install'], 'Preparing install for TSC and ESLint checks')
+    await spawnPromise('yarn', ['tsc'], 'Typescript checks')
+    await spawnPromise('yarn', ['eslint'], 'ESLint checks')
+    await spawnPromise('yarn', ['install', '--production'], 'Preparing install for production launch')
+    await spawnPromise('yarn', ['build'], 'Production build')
+    await spawnYarnStart('Production start')
+    await spawnPromise('yarn', ['install'], 'Testing successful! Reverting to development install')
+  } catch (e) {
+    console.error(e)
+  } finally {
+    process.exit(0)
+  }
+}
+
+main()