From 15cf781b69b66f48d08f8fdc602af8576b0ac6ab Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Tue, 26 Mar 2024 19:06:20 +0100 Subject: [PATCH 01/11] [feat]update mellow svelte to latest syntax (#93) * feat(mellow-react): move syntax to latest inertia-sails * feat(mellow-svelte): update to latest syntax From b926c90d6d9233446158e4be69a911425df739dc Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Wed, 10 Apr 2024 08:23:54 +0100 Subject: [PATCH 02/11] [feat] add create sails generator (#99) * feat(create-sails-generator): add basic generators * feat(create-sails-generator): add page generator * fix: remove unused util --- create-sails-generator/LICENSE | 21 ++++ create-sails-generator/README | 41 ++++++++ .../generators/bad-request/index.js | 24 +++++ .../bad-request/templates/badRequest.js | 95 +++++++++++++++++++ .../generators/inertia-redirect/index.js | 24 +++++ .../templates/inertiaRedirect.js | 20 ++++ .../generators/inertia/index.js | 24 +++++ .../generators/inertia/templates/inertia.js | 72 ++++++++++++++ .../generators/page/index.js | 70 ++++++++++++++ .../generators/page/templates/action.template | 15 +++ .../generators/page/templates/page.template | 19 ++++ create-sails-generator/index.js | 0 create-sails-generator/package.json | 22 +++++ .../tests/utils/get-action-name.test.js | 12 +++ .../tests/utils/get-component-name.test.js | 15 +++ .../utils/get-file-extension-for-ui.test.js | 15 +++ .../utils/get-action-name.js | 7 ++ .../utils/get-component-name.js | 11 +++ .../utils/get-file-extension-for-ui.js | 9 ++ .../utils/get-ui-framework.js | 13 +++ 20 files changed, 529 insertions(+) create mode 100644 create-sails-generator/LICENSE create mode 100644 create-sails-generator/README create mode 100644 create-sails-generator/generators/bad-request/index.js create mode 100644 create-sails-generator/generators/bad-request/templates/badRequest.js create mode 100644 create-sails-generator/generators/inertia-redirect/index.js create mode 100644 create-sails-generator/generators/inertia-redirect/templates/inertiaRedirect.js create mode 100644 create-sails-generator/generators/inertia/index.js create mode 100644 create-sails-generator/generators/inertia/templates/inertia.js create mode 100644 create-sails-generator/generators/page/index.js create mode 100644 create-sails-generator/generators/page/templates/action.template create mode 100644 create-sails-generator/generators/page/templates/page.template create mode 100644 create-sails-generator/index.js create mode 100644 create-sails-generator/package.json create mode 100644 create-sails-generator/tests/utils/get-action-name.test.js create mode 100644 create-sails-generator/tests/utils/get-component-name.test.js create mode 100644 create-sails-generator/tests/utils/get-file-extension-for-ui.test.js create mode 100644 create-sails-generator/utils/get-action-name.js create mode 100644 create-sails-generator/utils/get-component-name.js create mode 100644 create-sails-generator/utils/get-file-extension-for-ui.js create mode 100644 create-sails-generator/utils/get-ui-framework.js diff --git a/create-sails-generator/LICENSE b/create-sails-generator/LICENSE new file mode 100644 index 00000000..770d3b4e --- /dev/null +++ b/create-sails-generator/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 The Sailscasts Company + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/create-sails-generator/README b/create-sails-generator/README new file mode 100644 index 00000000..c823cec0 --- /dev/null +++ b/create-sails-generator/README @@ -0,0 +1,41 @@ +# create-sails-generator: A Sails Generator for The Boring JavaScript Stack + +This generator is specifically designed for The Boring JavaScript Stack, providing a plethora of commands for code generation and more to expedite your development process with The Boring JavaScript Stack. + +## Features + +- **Code Generation**: Quickly scaffold boilerplate code for pages, actions, and more. +- **Enhanced CLI Commands**: Extensive CLI commands tailored for Sails.js development. +- **Saves Time and Effort**: Automate repetitive tasks, saving you valuable development time. + +## Installation + +To use Create-Sails-Generator, you'll first need to have Node.js and npm installed on your machine. Then, follow these simple steps: + +1. Install Create-Sails-Generator globally via npm: + +```sh +npm i create-sails-generator --save-dev +``` + +2. Once installed, you can access the generator commands using the `sails generate` command. + +## Usage + +### Generating Files + +You can generate various files using the generator with simple commands. For instance, to create a new page, use: + +```sh +sails generate page dashboard +``` + +[Check out the docs](https://docs.sailscasts.com/boring-stack/generator) for more information on usage. + +## License + +`create-sails-generator` is licensed under the [MIT License](LICENSE.md). + +## About + +Create-Sails-Generator is maintained by The Sailscasts Company. It is part of [The Boring JavaScript Stack](https://docs.sailscasts.com/boring-stack) - The reliable full-stack JavaScript stack. diff --git a/create-sails-generator/generators/bad-request/index.js b/create-sails-generator/generators/bad-request/index.js new file mode 100644 index 00000000..b43c1b2a --- /dev/null +++ b/create-sails-generator/generators/bad-request/index.js @@ -0,0 +1,24 @@ +const path = require('path') +/** + * Generates responses/badRequest.js file + */ +module.exports = { + before: function (scope, done) { + if (scope.force !== false) { + scope.force = true + } + scope.relPath = 'badRequest.js' + return done() + }, + after: function (scope, done) { + console.log() + console.log(`Successfully generated ${scope.relPath}`) + console.log(' •-', `api/responses/${scope.relPath}`) + console.log() + return done() + }, + targets: { + './api/responses/:relPath': { copy: 'badRequest.js' } + }, + templatesDirectory: path.resolve(__dirname, './templates') +} diff --git a/create-sails-generator/generators/bad-request/templates/badRequest.js b/create-sails-generator/generators/bad-request/templates/badRequest.js new file mode 100644 index 00000000..142fcd6b --- /dev/null +++ b/create-sails-generator/generators/bad-request/templates/badRequest.js @@ -0,0 +1,95 @@ +/** + * badRequest.js + * + * A custom response. + * + * Example usage: + * ``` + * return res.badRequest(); + * // -or- + * return res.badRequest(optionalData); + * ``` + * + * Or with actions2: + * ``` + * exits: { + * somethingHappened: { + * responseType: 'badRequest' + * } + * } + * ``` + * + * ``` + * throw 'somethingHappened'; + * // -or- + * throw { somethingHappened: optionalData } + * ``` + */ + +module.exports = function badRequest(optionalData) { + // Get access to `req` and `res` + const req = this.req + const res = this.res + + // Define the status code to send in the response. + const statusCodeToSet = 400 + + // Check if it's an Inertia request + if (req.header('X-Inertia')) { + if (optionalData && optionalData.problems) { + const errors = {} + optionalData.problems.forEach((problem) => { + if (typeof problem === 'object') { + Object.keys(problem).forEach((propertyName) => { + const sanitizedProblem = problem[propertyName].replace(/\.$/, '') // Trim trailing dot + if (!errors[propertyName]) { + errors[propertyName] = [sanitizedProblem] + } else { + errors[propertyName].push(sanitizedProblem) + } + }) + } else { + const regex = /"(.*?)"/ + const matches = problem.match(regex) + + if (matches && matches.length > 1) { + const propertyName = matches[1] + const sanitizedProblem = problem + .replace(/"([^"]+)"/, '$1') + .replace('\n', '') + .replace('·', '') + .trim() + if (!errors[propertyName]) { + errors[propertyName] = [sanitizedProblem] + } else { + errors[propertyName].push(sanitizedProblem) + } + } + } + }) + req.session.errors = errors + return res.redirect(303, 'back') + } + } + + // If not an Inertia request, perform the normal badRequest response + if (optionalData === undefined) { + sails.log.info('Ran custom response: res.badRequest()') + return res.sendStatus(statusCodeToSet) + } else if (_.isError(optionalData)) { + sails.log.info( + 'Custom response `res.badRequest()` called with an Error:', + optionalData + ) + + if (!_.isFunction(optionalData.toJSON)) { + if (process.env.NODE_ENV === 'production') { + return res.sendStatus(statusCodeToSet) + } else { + return res.status(statusCodeToSet).send(optionalData.stack) + } + } + } else { + return res.status(statusCodeToSet).send(optionalData) + } +} diff --git a/create-sails-generator/generators/inertia-redirect/index.js b/create-sails-generator/generators/inertia-redirect/index.js new file mode 100644 index 00000000..14ed58e0 --- /dev/null +++ b/create-sails-generator/generators/inertia-redirect/index.js @@ -0,0 +1,24 @@ +const path = require('path') +/** + * Generates responses/inertia.js file + */ +module.exports = { + before: function (scope, done) { + if (scope.force !== false) { + scope.force = true + } + scope.relPath = 'inertiaRedirect.js' + return done() + }, + after: function (scope, done) { + console.log() + console.log(`Successfully generated ${scope.relPath}`) + console.log(' •-', `api/responses/${scope.relPath}`) + console.log() + return done() + }, + targets: { + './api/responses/:relPath': { copy: 'inertiaRedirect.js' } + }, + templatesDirectory: path.resolve(__dirname, './templates') +} diff --git a/create-sails-generator/generators/inertia-redirect/templates/inertiaRedirect.js b/create-sails-generator/generators/inertia-redirect/templates/inertiaRedirect.js new file mode 100644 index 00000000..dc44d1f2 --- /dev/null +++ b/create-sails-generator/generators/inertia-redirect/templates/inertiaRedirect.js @@ -0,0 +1,20 @@ +// @ts-nocheck + +const inertiaHeaders = { + INERTIA: 'X-Inertia', + LOCATION: 'X-Inertia-Location' +} + +module.exports = function inertiaRedirect(url) { + const req = this.req + const res = this.res + + if (req.get(inertiaHeaders.INERTIA)) { + res.set(inertiaHeaders.LOCATION, url) + } + + return res.redirect( + ['PUT', 'PATCH', 'DELETE'].includes(req.method) ? 303 : 409, + url + ) +} diff --git a/create-sails-generator/generators/inertia/index.js b/create-sails-generator/generators/inertia/index.js new file mode 100644 index 00000000..36467083 --- /dev/null +++ b/create-sails-generator/generators/inertia/index.js @@ -0,0 +1,24 @@ +const path = require('path') +/** + * Generates responses/inertia.js file + */ +module.exports = { + before: function (scope, done) { + if (scope.force !== false) { + scope.force = true + } + scope.relPath = 'inertia.js' + return done() + }, + after: function (scope, done) { + console.log() + console.log(`Successfully generated ${scope.relPath}`) + console.log(' •-', `api/responses/${scope.relPath}`) + console.log() + return done() + }, + targets: { + './api/responses/:relPath': { copy: 'inertia.js' } + }, + templatesDirectory: path.resolve(__dirname, './templates') +} diff --git a/create-sails-generator/generators/inertia/templates/inertia.js b/create-sails-generator/generators/inertia/templates/inertia.js new file mode 100644 index 00000000..f5142989 --- /dev/null +++ b/create-sails-generator/generators/inertia/templates/inertia.js @@ -0,0 +1,72 @@ +// @ts-nocheck +const { encode } = require('querystring') +module.exports = function inertia(data) { + const req = this.req + const res = this.res + const sails = req._sails + + const sharedProps = sails.inertia.sharedProps + const sharedViewData = sails.inertia.sharedViewData + const rootView = sails.config.inertia.rootView + + const allProps = { + ...sharedProps, + ...data.props + } + + const allViewData = { + ...sharedViewData, + ...data.viewData + } + + let url = req.url || req.originalUrl + const assetVersion = sails.config.inertia.version + const currentVersion = + typeof assetVersion === 'function' ? assetVersion() : assetVersion + + const page = { + component: data.page, + version: currentVersion, + props: allProps, + url + } + + // Implements inertia partial reload. See https://inertiajs.com/partial-reload + if ( + req.get(inertiaHeaders.PARTIAL_DATA) && + req.get(inertiaHeaders.PARTIAL_COMPONENT) === page.component + ) { + const only = req.get(inertiaHeaders.PARTIAL_DATA).split(',') + page.props = only.length ? getPartialData(data.props, only) : page.props + } + + const queryParams = req.query + if (req.method === 'GET' && Object.keys(queryParams).length) { + // Keep original request query params + url += `?${encode(queryParams)}` + } + + if (req.get(inertiaHeaders.INERTIA)) { + res.set(inertiaHeaders.INERTIA, true) + res.set('Vary', 'Accept') + return res.json(page) + } else { + // Implements full page reload + return res.view(rootView, { + page, + viewData: allViewData + }) + } +} + +function getPartialData(props, only = []) { + return Object.assign({}, ...only.map((key) => ({ [key]: props[key] }))) +} + +const inertiaHeaders = { + INERTIA: 'X-Inertia', + VERSION: 'X-Inertia-Version', + PARTIAL_DATA: 'X-Inertia-Partial-Data', + PARTIAL_COMPONENT: 'X-Inertia-Partial-Component', + LOCATION: 'X-Inertia-Location' +} diff --git a/create-sails-generator/generators/page/index.js b/create-sails-generator/generators/page/index.js new file mode 100644 index 00000000..812d5c69 --- /dev/null +++ b/create-sails-generator/generators/page/index.js @@ -0,0 +1,70 @@ +const path = require('path') +const getUiFramework = require('../../utils/get-ui-framework') +const getComponentName = require('../../utils/get-component-name') +const getFileExtensionForUi = require('../../utils/get-file-extension-for-ui') +const getActionName = require('../../utils/get-action-name') +/** + * Generates responses/inertia.js file + */ + +module.exports = { + before: function (scope, done) { + const appPackageJSON = require(`${scope.topLvlRootPath}/package.json`) + const uiFramework = getUiFramework(appPackageJSON) + let roughName + + if (scope.name) { + roughName = scope.name + } + + if (scope.args[0] && typeof scope.args[0] == 'string') { + roughName = scope.args[0] + } + + if (!roughName) { + return done( + new Error( + 'Missing argument: Please provide a name for the new page.\n' + + '(e.g. `profile` or `sign-up`).' + ) + ) + } + + // Replace backslashes with proper slashes. + // (This is crucial for Windows compatibility.) + roughName = roughName.replace(/\\/g, '/') + + scope.pageRelPath = roughName.replace(/\.+/g, '/') + scope.pagePath = scope.pageRelPath + console.log(scope.pagePath) + scope.pageRelPath += getFileExtensionForUi(uiFramework) + scope.uiFramework = uiFramework + if (uiFramework == 'react') { + scope.componentName = getComponentName(roughName) + } + + scope.actionRelPath = getActionName(roughName) + scope.actionRelPath += '.js' + scope.actionFriendlyName = `View ${scope.pagePath}` + scope.actionDescription = `Display ${scope.pagePath} page` + + return done() + }, + after: function (scope, done) { + console.log() + + console.log(`Successfully generated ${scope.pageRelPath}`) + console.log(' •-', `assets/js/pages/${scope.pageRelPath}`) + + console.log(`Successfully generated ${scope.actionRelPath}`) + console.log(' •-', `api/controllers/${scope.actionRelPath}`) + + console.log() + return done() + }, + targets: { + './assets/js/pages/:pageRelPath': { template: 'page.template' }, + './api/controllers/:actionRelPath': { template: 'action.template' } + }, + templatesDirectory: path.resolve(__dirname, './templates') +} diff --git a/create-sails-generator/generators/page/templates/action.template b/create-sails-generator/generators/page/templates/action.template new file mode 100644 index 00000000..f725da7b --- /dev/null +++ b/create-sails-generator/generators/page/templates/action.template @@ -0,0 +1,15 @@ +module.exports = { + friendlyName: <%= util.inspect(actionFriendlyName) %>, + + description: <%= util.inspect(actionDescription) %>, + + exits: { + success: { + responseType: 'inertia' + } + }, + + fn: async function () { + return { page: <%= util.inspect(pagePath) %> } + } +} diff --git a/create-sails-generator/generators/page/templates/page.template b/create-sails-generator/generators/page/templates/page.template new file mode 100644 index 00000000..e680276b --- /dev/null +++ b/create-sails-generator/generators/page/templates/page.template @@ -0,0 +1,19 @@ +<% if (uiFramework == 'vue') {%> + + + +<% } else if (uiFramework == 'react') { %> +import { Head, Link, useForm, usePage } from '@inertiajs/react' + +export default function <%= componentName %>(props) { + return
+} +<% } else if (uiFramework == 'svelte') { %> + + +
+<% } %> diff --git a/create-sails-generator/index.js b/create-sails-generator/index.js new file mode 100644 index 00000000..e69de29b diff --git a/create-sails-generator/package.json b/create-sails-generator/package.json new file mode 100644 index 00000000..5815a0be --- /dev/null +++ b/create-sails-generator/package.json @@ -0,0 +1,22 @@ +{ + "name": "create-sails-generator", + "version": "0.0.0", + "description": "Sails generator for The Boring JavaScript Stack.", + "scripts": { + "test": "node --test" + }, + "keywords": [ + "The Boring JavaScript Stack", + "sails", + "senerator" + ], + "author": "Kelvin Omereshone", + "license": "MIT", + "peerDependencies": { + "sails": ">=1" + }, + "bugs": { + "url": "https://github.com/sailscastshq/boring-stack/issues" + }, + "homepage": "https://github.com/sailscastshq/boring-stack/create-sails-generator#readme" +} diff --git a/create-sails-generator/tests/utils/get-action-name.test.js b/create-sails-generator/tests/utils/get-action-name.test.js new file mode 100644 index 00000000..732b3305 --- /dev/null +++ b/create-sails-generator/tests/utils/get-action-name.test.js @@ -0,0 +1,12 @@ +const { describe, it } = require('node:test') +const assert = require('node:assert/strict') +const getActionName = require('../../utils/get-action-name') + +describe('getActionName', function () { + it("returns 'view-dashboard' when given 'dashboard'", function () { + assert.equal(getActionName('dashboard'), 'view-dashboard') + }) + it("returns 'user/view-users' when given 'users/index'", function () { + assert.equal(getActionName('users/index'), 'user/view-users') + }) +}) diff --git a/create-sails-generator/tests/utils/get-component-name.test.js b/create-sails-generator/tests/utils/get-component-name.test.js new file mode 100644 index 00000000..92877174 --- /dev/null +++ b/create-sails-generator/tests/utils/get-component-name.test.js @@ -0,0 +1,15 @@ +const { describe, it } = require('node:test') +const assert = require('node:assert/strict') +const getComponentName = require('../../utils/get-component-name') + +describe('getComponentName', function () { + it("returns 'Profile' when given 'profile'", function () { + assert.equal(getComponentName('profile'), 'Profile') + }) + it("returns 'UserDashboard' when given 'user-dashboard'", function () { + assert.equal(getComponentName('user-dashboard'), 'UserDashboard') + }) + it("returns 'ExampleNewPage' when given 'example/new-page'", function () { + assert.equal(getComponentName('example/new-page'), 'ExampleNewPage') + }) +}) diff --git a/create-sails-generator/tests/utils/get-file-extension-for-ui.test.js b/create-sails-generator/tests/utils/get-file-extension-for-ui.test.js new file mode 100644 index 00000000..873abc70 --- /dev/null +++ b/create-sails-generator/tests/utils/get-file-extension-for-ui.test.js @@ -0,0 +1,15 @@ +const { describe, it } = require('node:test') +const assert = require('node:assert/strict') +const getFileExtensionForUI = require('../../utils/get-file-extension-for-ui') + +describe('getFileExtensionForUI', function () { + it("returns '.vue' when given 'vue'", function () { + assert.equal(getFileExtensionForUI('vue'), '.vue') + }) + it("returns '.jsx' when given 'react'", function () { + assert.equal(getFileExtensionForUI('react'), '.jsx') + }) + it("returns '.svelte' when given 'svelte'", function () { + assert.equal(getFileExtensionForUI('svelte'), '.svelte') + }) +}) diff --git a/create-sails-generator/utils/get-action-name.js b/create-sails-generator/utils/get-action-name.js new file mode 100644 index 00000000..8167db30 --- /dev/null +++ b/create-sails-generator/utils/get-action-name.js @@ -0,0 +1,7 @@ +module.exports = function getActionName(pageName) { + if (!pageName.includes('/')) return 'view-' + pageName + const [firstPart] = pageName.split('/') + const actionName = 'view-' + firstPart + const singularizedFirsPart = firstPart.replace(/s$/, '') + return `${singularizedFirsPart}/${actionName}` +} diff --git a/create-sails-generator/utils/get-component-name.js b/create-sails-generator/utils/get-component-name.js new file mode 100644 index 00000000..572969a5 --- /dev/null +++ b/create-sails-generator/utils/get-component-name.js @@ -0,0 +1,11 @@ +module.exports = function getComponentName(str) { + const words = str.split(/-|\//) + + return words + .map((word, index) => + index === 0 + ? word.charAt(0).toUpperCase() + word.slice(1) + : word.charAt(0).toUpperCase() + word.slice(1) + ) + .join('') +} diff --git a/create-sails-generator/utils/get-file-extension-for-ui.js b/create-sails-generator/utils/get-file-extension-for-ui.js new file mode 100644 index 00000000..1a4da6aa --- /dev/null +++ b/create-sails-generator/utils/get-file-extension-for-ui.js @@ -0,0 +1,9 @@ +module.exports = function getFileExtensionForUI(uiFramework) { + const extensions = { + vue: '.vue', + react: '.jsx', + svelte: '.svelte' + } + + return extensions[uiFramework] +} diff --git a/create-sails-generator/utils/get-ui-framework.js b/create-sails-generator/utils/get-ui-framework.js new file mode 100644 index 00000000..120a5913 --- /dev/null +++ b/create-sails-generator/utils/get-ui-framework.js @@ -0,0 +1,13 @@ +module.exports = function getUIFramework(packageJSON) { + if (!packageJSON.dependencies) + throw new Error('Package.json do not have dependencies') + if (packageJSON.dependencies['@inertiajs/vue3']) { + return 'vue' + } else if (packageJSON.dependencies['@inertiajs/react']) { + return 'react' + } else if (packageJSON.dependencies['@inertiajs/svelte']) { + return 'svelte' + } else { + throw new Error('Could not get UI framework from package.json.') + } +} From 1674e7c480a17937e94df012a2c590385e7a2e83 Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Fri, 29 Mar 2024 21:45:00 +0100 Subject: [PATCH 03/11] feat(create-sails-generator): add basic generators --- .../generators/page/templates/react.template | 6 ++ .../generators/page/templates/svelte.template | 5 ++ .../generators/page/templates/vue.template | 4 ++ create-sails-generator/utils/copy.js | 66 +++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 create-sails-generator/generators/page/templates/react.template create mode 100644 create-sails-generator/generators/page/templates/svelte.template create mode 100644 create-sails-generator/generators/page/templates/vue.template create mode 100644 create-sails-generator/utils/copy.js diff --git a/create-sails-generator/generators/page/templates/react.template b/create-sails-generator/generators/page/templates/react.template new file mode 100644 index 00000000..95ef241c --- /dev/null +++ b/create-sails-generator/generators/page/templates/react.template @@ -0,0 +1,6 @@ + +import { Head, Link, useForm, usePage } from '@inertiajs/react' + +export default function <%= util.inspect(componentName) %>(props) { + return
+} diff --git a/create-sails-generator/generators/page/templates/svelte.template b/create-sails-generator/generators/page/templates/svelte.template new file mode 100644 index 00000000..945768c9 --- /dev/null +++ b/create-sails-generator/generators/page/templates/svelte.template @@ -0,0 +1,5 @@ + + +
diff --git a/create-sails-generator/generators/page/templates/vue.template b/create-sails-generator/generators/page/templates/vue.template new file mode 100644 index 00000000..6f4cbdde --- /dev/null +++ b/create-sails-generator/generators/page/templates/vue.template @@ -0,0 +1,4 @@ + + diff --git a/create-sails-generator/utils/copy.js b/create-sails-generator/utils/copy.js new file mode 100644 index 00000000..79ea41d5 --- /dev/null +++ b/create-sails-generator/utils/copy.js @@ -0,0 +1,66 @@ +const fs = require('fs') +const path = require('path') + +/** + * Copy file from one place to another. + * + * @param {Object} options + * @param {String} options.rootPath The absolute path of the destination for the copied file. + * @param {String} options.templatePath The relative path from the generator's `templates/` dir to the source template whose contents will be copied. + * @param {String} options.templatesDirectory An absolute path to the generator's `templates/` dir. + * @param {Boolean} [options.force=false] Whether to overwrite existing files. + * @param {Function} cb Callback function (err) + */ +module.exports = function copyFile(options, cb) { + try { + if ( + !options || + typeof options !== 'object' || + !options.templatePath || + !options.templatesDirectory || + !options.rootPath + ) { + throw new Error( + 'Invalid options. `templatePath`, `templatesDirectory`, and `rootPath` are required.' + ) + } + options.force = options.force || false + + const absSrcPath = path.resolve( + options.templatesDirectory, + options.templatePath + ) + + fs.mkdir(path.dirname(options.rootPath), { recursive: true }, (err) => { + if (err) { + return cb(err) + } + // Check if the destination file exists + fs.access(options.rootPath, fs.constants.F_OK, (err) => { + if (!err && !options.force) { + return cb( + new Error(`File already exists at destination: ${options.rootPath}`) + ) + } + + // Remove the destination file if it exists and force option is true + if (options.force) { + fs.unlink(options.rootPath, (err) => { + if (err && err.code !== 'ENOENT') { + return cb(err) + } + }) + } + }) + + fs.copyFile(absSrcPath, options.rootPath, (err) => { + if (err) { + return cb(err) + } + cb() + }) + }) + } catch (err) { + cb(err) + } +} From dd534ad70eedc2209831a7c41a3ff1ae38950f27 Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Wed, 10 Apr 2024 08:18:41 +0100 Subject: [PATCH 04/11] feat(create-sails-generator): add page generator --- .../generators/page/templates/react.template | 6 ------ .../generators/page/templates/svelte.template | 5 ----- .../generators/page/templates/vue.template | 4 ---- 3 files changed, 15 deletions(-) delete mode 100644 create-sails-generator/generators/page/templates/react.template delete mode 100644 create-sails-generator/generators/page/templates/svelte.template delete mode 100644 create-sails-generator/generators/page/templates/vue.template diff --git a/create-sails-generator/generators/page/templates/react.template b/create-sails-generator/generators/page/templates/react.template deleted file mode 100644 index 95ef241c..00000000 --- a/create-sails-generator/generators/page/templates/react.template +++ /dev/null @@ -1,6 +0,0 @@ - -import { Head, Link, useForm, usePage } from '@inertiajs/react' - -export default function <%= util.inspect(componentName) %>(props) { - return
-} diff --git a/create-sails-generator/generators/page/templates/svelte.template b/create-sails-generator/generators/page/templates/svelte.template deleted file mode 100644 index 945768c9..00000000 --- a/create-sails-generator/generators/page/templates/svelte.template +++ /dev/null @@ -1,5 +0,0 @@ - - -
diff --git a/create-sails-generator/generators/page/templates/vue.template b/create-sails-generator/generators/page/templates/vue.template deleted file mode 100644 index 6f4cbdde..00000000 --- a/create-sails-generator/generators/page/templates/vue.template +++ /dev/null @@ -1,4 +0,0 @@ - - From 50e86add223cb40df3a71592a6def2bb181695fd Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Wed, 10 Apr 2024 08:21:31 +0100 Subject: [PATCH 05/11] fix: remove unused util --- create-sails-generator/utils/copy.js | 66 ---------------------------- 1 file changed, 66 deletions(-) delete mode 100644 create-sails-generator/utils/copy.js diff --git a/create-sails-generator/utils/copy.js b/create-sails-generator/utils/copy.js deleted file mode 100644 index 79ea41d5..00000000 --- a/create-sails-generator/utils/copy.js +++ /dev/null @@ -1,66 +0,0 @@ -const fs = require('fs') -const path = require('path') - -/** - * Copy file from one place to another. - * - * @param {Object} options - * @param {String} options.rootPath The absolute path of the destination for the copied file. - * @param {String} options.templatePath The relative path from the generator's `templates/` dir to the source template whose contents will be copied. - * @param {String} options.templatesDirectory An absolute path to the generator's `templates/` dir. - * @param {Boolean} [options.force=false] Whether to overwrite existing files. - * @param {Function} cb Callback function (err) - */ -module.exports = function copyFile(options, cb) { - try { - if ( - !options || - typeof options !== 'object' || - !options.templatePath || - !options.templatesDirectory || - !options.rootPath - ) { - throw new Error( - 'Invalid options. `templatePath`, `templatesDirectory`, and `rootPath` are required.' - ) - } - options.force = options.force || false - - const absSrcPath = path.resolve( - options.templatesDirectory, - options.templatePath - ) - - fs.mkdir(path.dirname(options.rootPath), { recursive: true }, (err) => { - if (err) { - return cb(err) - } - // Check if the destination file exists - fs.access(options.rootPath, fs.constants.F_OK, (err) => { - if (!err && !options.force) { - return cb( - new Error(`File already exists at destination: ${options.rootPath}`) - ) - } - - // Remove the destination file if it exists and force option is true - if (options.force) { - fs.unlink(options.rootPath, (err) => { - if (err && err.code !== 'ENOENT') { - return cb(err) - } - }) - } - }) - - fs.copyFile(absSrcPath, options.rootPath, (err) => { - if (err) { - return cb(err) - } - cb() - }) - }) - } catch (err) { - cb(err) - } -} From 2b08d549ff32f15c34a43a2cc43d916e779ab680 Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Wed, 10 Apr 2024 08:26:48 +0100 Subject: [PATCH 06/11] chore: bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index eadcbfdd..ca0b68eb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "boring-stack", - "version": "0.2.1", + "version": "0.2.2", "private": "true", "description": "The Boring JavaScript Stack 🥱 - an opinionated project starter for fullstack JavaScript", "scripts": { From 4c67bb2c69b36db228edb765ac616a5ac99f87d4 Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Wed, 10 Apr 2024 08:27:14 +0100 Subject: [PATCH 07/11] chore(create-sails-generator): bump version --- create-sails-generator/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/create-sails-generator/package.json b/create-sails-generator/package.json index 5815a0be..8c164c58 100644 --- a/create-sails-generator/package.json +++ b/create-sails-generator/package.json @@ -1,6 +1,6 @@ { "name": "create-sails-generator", - "version": "0.0.0", + "version": "0.0.1", "description": "Sails generator for The Boring JavaScript Stack.", "scripts": { "test": "node --test" From 0299c11cdeca2ce0eeb2723c32a0006691628c08 Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Wed, 10 Apr 2024 08:58:56 +0100 Subject: [PATCH 08/11] feat(mellow-vue): setup create-sails-generator --- templates/mellow-vue/.sailsrc | 7 ++++++- templates/mellow-vue/package.json | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/templates/mellow-vue/.sailsrc b/templates/mellow-vue/.sailsrc index b3e7f576..6fe74d61 100644 --- a/templates/mellow-vue/.sailsrc +++ b/templates/mellow-vue/.sailsrc @@ -1,6 +1,11 @@ { "generators": { - "modules": {} + "modules": { + "page": "create-sails-generator/generators/page", + "inertia": "create-sails-generator/generators/inertia", + "inertia-redirect": "create-sails-generator/generators/inertia-redirect", + "bad-request": "create-sails-generator/generators/bad-request" + } }, "_generatedWith": { "sails": "1.5.2", diff --git a/templates/mellow-vue/package.json b/templates/mellow-vue/package.json index 0d02301b..1297fed7 100644 --- a/templates/mellow-vue/package.json +++ b/templates/mellow-vue/package.json @@ -32,7 +32,8 @@ "sails-hook-shipwright": "^0.0.1", "sails.io.js": "^1.2.1", "socket.io-client": "^2.2.0", - "tailwindcss": "^3.0.24" + "tailwindcss": "^3.0.24", + "create-sails-generator": "^0.0.1" }, "scripts": { "dev": "node --watch app.js", From 46d75e714954cb9f14b4d52420b26b2d12a7bc3c Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Wed, 10 Apr 2024 09:00:26 +0100 Subject: [PATCH 09/11] feat(mellow-svelte): setup create-sails-generator --- templates/mellow-svelte/.sailsrc | 7 ++++++- templates/mellow-svelte/package.json | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/templates/mellow-svelte/.sailsrc b/templates/mellow-svelte/.sailsrc index b3e7f576..6fe74d61 100644 --- a/templates/mellow-svelte/.sailsrc +++ b/templates/mellow-svelte/.sailsrc @@ -1,6 +1,11 @@ { "generators": { - "modules": {} + "modules": { + "page": "create-sails-generator/generators/page", + "inertia": "create-sails-generator/generators/inertia", + "inertia-redirect": "create-sails-generator/generators/inertia-redirect", + "bad-request": "create-sails-generator/generators/bad-request" + } }, "_generatedWith": { "sails": "1.5.2", diff --git a/templates/mellow-svelte/package.json b/templates/mellow-svelte/package.json index 0f6e4ae6..a8634251 100644 --- a/templates/mellow-svelte/package.json +++ b/templates/mellow-svelte/package.json @@ -24,7 +24,8 @@ "sails-hook-shipwright": "^0.0.1", "sails.io.js": "^1.2.1", "socket.io-client": "^2.2.0", - "tailwindcss": "^3.0.24" + "tailwindcss": "^3.0.24", + "create-sails-generator": "^0.0.1" }, "scripts": { "dev": "node app.js", From 4f121e4474af576b6336c7470522b2ceee738b2f Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Wed, 10 Apr 2024 09:03:46 +0100 Subject: [PATCH 10/11] feat(mellow-react): add setup for create-sails-generator --- templates/mellow-react/.sailsrc | 7 ++++++- templates/mellow-react/package.json | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/templates/mellow-react/.sailsrc b/templates/mellow-react/.sailsrc index b3e7f576..6fe74d61 100644 --- a/templates/mellow-react/.sailsrc +++ b/templates/mellow-react/.sailsrc @@ -1,6 +1,11 @@ { "generators": { - "modules": {} + "modules": { + "page": "create-sails-generator/generators/page", + "inertia": "create-sails-generator/generators/inertia", + "inertia-redirect": "create-sails-generator/generators/inertia-redirect", + "bad-request": "create-sails-generator/generators/bad-request" + } }, "_generatedWith": { "sails": "1.5.2", diff --git a/templates/mellow-react/package.json b/templates/mellow-react/package.json index f19ce686..eb2507da 100644 --- a/templates/mellow-react/package.json +++ b/templates/mellow-react/package.json @@ -25,7 +25,8 @@ "sails-hook-shipwright": "^0.0.1", "sails.io.js": "^1.2.1", "socket.io-client": "^2.2.0", - "tailwindcss": "^3.0.24" + "tailwindcss": "^3.0.24", + "create-sails-generator": "^0.0.1" }, "scripts": { "dev": "node app.js", From 285dfb3a9d91c087309bd3f488eb46d51a61deb7 Mon Sep 17 00:00:00 2001 From: Kelvin Oghenerhoro Omereshone Date: Wed, 10 Apr 2024 09:04:50 +0100 Subject: [PATCH 11/11] chore: release 0.2.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ca0b68eb..c335bacf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "boring-stack", - "version": "0.2.2", + "version": "0.2.3", "private": "true", "description": "The Boring JavaScript Stack 🥱 - an opinionated project starter for fullstack JavaScript", "scripts": {