From 4667b9906c0b43b0429ac8760da9e7dca644b54c Mon Sep 17 00:00:00 2001 From: Mike Peters Date: Mon, 28 Feb 2022 20:53:15 -0800 Subject: [PATCH] refactor: reorganize files - move all code into lib folder - move unit tests next to lib code - rename files in kebab-case - sort some requires - remove utilities folder since there was only one file --- .nycrc.json | 2 +- lib/{bucketUtils.js => bucket-utils.js} | 11 ++++++----- lib/configure.js | 2 +- .../getFileList.js => get-file-list.js} | 0 index.js => lib/plugin.js | 15 +++++++-------- test/unit/deploy.test.js => lib/plugin.test.js | 2 +- .../{awsRegionUrls.json => aws-region-urls.json} | 0 .../{CORSPolicy.json => cors-policy.json} | 0 lib/upload.js | 9 +++------ lib/validate.js | 3 +-- package.json | 4 ++-- 11 files changed, 22 insertions(+), 26 deletions(-) rename lib/{bucketUtils.js => bucket-utils.js} (91%) rename lib/{utilities/getFileList.js => get-file-list.js} (100%) rename index.js => lib/plugin.js (97%) rename test/unit/deploy.test.js => lib/plugin.test.js (99%) rename lib/resources/{awsRegionUrls.json => aws-region-urls.json} (100%) rename lib/resources/{CORSPolicy.json => cors-policy.json} (100%) diff --git a/.nycrc.json b/.nycrc.json index 0b39f56..50167c6 100644 --- a/.nycrc.json +++ b/.nycrc.json @@ -1,6 +1,6 @@ { "all": true, - "include": ["index.js", "lib/**/*.js"], + "include": ["lib/**/*.js"], "exclude": ["**/*.test.js"], "reporter": ["lcov", "html", "text-summary"] } diff --git a/lib/bucketUtils.js b/lib/bucket-utils.js similarity index 91% rename from lib/bucketUtils.js rename to lib/bucket-utils.js index 2323298..1b74caf 100644 --- a/lib/bucketUtils.js +++ b/lib/bucket-utils.js @@ -56,17 +56,18 @@ function emptyBucket(aws, bucketName, keyPrefix) { if (!contents[0]) { return Promise.resolve(); } else { - if (keyPrefix) { testPrefix = true; prefixRegexp = new RegExp('^' + keyPrefix); } - const objects = contents.map(function(content) { - return {Key: content.Key}; - }).filter(content => !testPrefix || prefixRegexp.test(content.Key)); + const objects = contents + .map(function (content) { + return { Key: content.Key }; + }) + .filter(content => !testPrefix || prefixRegexp.test(content.Key)); if (objects.length === 0) { - return Promise.resolve() + return Promise.resolve(); } const params = { diff --git a/lib/configure.js b/lib/configure.js index 5b8be11..45ef0e3 100644 --- a/lib/configure.js +++ b/lib/configure.js @@ -122,7 +122,7 @@ function configurePolicyForBucket(aws, bucketName, customPolicy) { function configureCorsForBucket(aws, bucketName) { const params = { Bucket: bucketName, - CORSConfiguration: require('./resources/CORSPolicy') + CORSConfiguration: require('./resources/cors-policy.json') }; return aws.request('S3', 'putBucketCors', params); diff --git a/lib/utilities/getFileList.js b/lib/get-file-list.js similarity index 100% rename from lib/utilities/getFileList.js rename to lib/get-file-list.js diff --git a/index.js b/lib/plugin.js similarity index 97% rename from index.js rename to lib/plugin.js index 6c6a0ce..bd65139 100644 --- a/index.js +++ b/lib/plugin.js @@ -1,15 +1,14 @@ 'use strict'; -const path = require('path'); -const fs = require('fs'); - const confirm = require('@serverless/utils/inquirer/confirm'); +const fs = require('fs'); +const path = require('path'); -const bucketUtils = require('./lib/bucketUtils'); -const configure = require('./lib/configure'); -const regionUrls = require('./lib/resources/awsRegionUrls'); -const uploadDirectory = require('./lib/upload'); -const validateClient = require('./lib/validate'); +const bucketUtils = require('./bucket-utils'); +const configure = require('./configure'); +const regionUrls = require('./resources/aws-region-urls.json'); +const uploadDirectory = require('./upload'); +const validateClient = require('./validate'); class Client { constructor(serverless, cliOptions, { log }) { diff --git a/test/unit/deploy.test.js b/lib/plugin.test.js similarity index 99% rename from test/unit/deploy.test.js rename to lib/plugin.test.js index 5596a35..9a68aaa 100644 --- a/test/unit/deploy.test.js +++ b/lib/plugin.test.js @@ -5,7 +5,7 @@ const inquirer = require('@serverless/utils/inquirer'); const chai = require('chai'); const sinon = require('sinon'); -const runServerless = require('../run-serverless'); +const runServerless = require('../test/run-serverless'); chai.use(require('chai-as-promised')); chai.use(require('sinon-chai')); diff --git a/lib/resources/awsRegionUrls.json b/lib/resources/aws-region-urls.json similarity index 100% rename from lib/resources/awsRegionUrls.json rename to lib/resources/aws-region-urls.json diff --git a/lib/resources/CORSPolicy.json b/lib/resources/cors-policy.json similarity index 100% rename from lib/resources/CORSPolicy.json rename to lib/resources/cors-policy.json diff --git a/lib/upload.js b/lib/upload.js index f5c2daf..03af020 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -1,14 +1,11 @@ 'use strict'; const fs = require('fs'); -const path = require('path'); - const mime = require('mime'); - -const getFileList = require('./utilities/getFileList'); -const regionUrls = require('./resources/awsRegionUrls'); - const minimatch = require('minimatch'); +const path = require('path'); + +const getFileList = require('./get-file-list'); /** * Uploads client files to an S3 bucket diff --git a/lib/validate.js b/lib/validate.js index a73cdc9..4b19a5c 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -1,9 +1,8 @@ 'use strict'; const fs = require('fs'); -const path = require('path'); - const is = require('is_js'); +const path = require('path'); /** * Validates the configuration parameters that will be used for deployment diff --git a/package.json b/package.json index 037e302..55b13b8 100644 --- a/package.json +++ b/package.json @@ -5,10 +5,10 @@ "node": ">=14.15.5" }, "description": "Deploy your serverless static website to AWS S3.", - "main": "index.js", + "main": "lib/plugin.js", "scripts": { "prepare": "husky install", - "test": "mocha \"test/unit/**/*.test.js\"", + "test": "mocha \"lib/**/*.test.js\"", "coverage": "nyc npm run test", "integration-test": "mocha-isolated --pass-through-aws-creds \"test/integration/**/*.test.js\"" },