Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7,897 changes: 7,897 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

51 changes: 27 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,42 @@
"raisely": "./bin/raisely.js"
},
"scripts": {
"test": "node --test"
"test": "vitest run --config vitest.config.js"
},
"engines": {
"node": ">=18"
},
"author": "Raisely",
"license": "SEE LICENSE IN LICENSE.md",
"dependencies": {
"@babel/core": "^7.17.8",
"@babel/plugin-proposal-class-properties": "^7.16.7",
"@babel/preset-env": "^7.16.11",
"@babel/preset-react": "^7.16.7",
"@napi-rs/keyring": "^1.2.0",
"chalk": "^2.4.2",
"commander": "^9.2.0",
"express": "^4.17.3",
"folder-hash": "^4.0.2",
"fzstd": "^0.1.1",
"glob": "^7.1.6",
"glob-promise": "^3.4.0",
"handlebars": "4.7.7",
"http-proxy-middleware": "^2.0.4",
"inquirer": "^6.3.1",
"jwt-decode": "^3.0.0-beta.2",
"lodash": "^4.17.11",
"node-fetch": "^3.2.3",
"node-watch": "^0.7.1",
"open": "^8.4.0",
"ora": "^3.4.0",
"p-limit": "^4.0.0"
"@babel/core": "7.12.10",
"@babel/helpers": "7.12.5",
"@babel/plugin-proposal-class-properties": "7.12.1",
"@babel/plugin-transform-regenerator": "7.12.1",
"@babel/preset-env": "7.12.11",
"@babel/preset-react": "7.12.10",
"@napi-rs/keyring": "1.3.0",
"chalk": "5.6.2",
"commander": "13.1.0",
"express": "5.2.1",
"folder-hash": "4.1.2",
"fzstd": "0.1.1",
"glob": "8.1.0",
"glob-promise": "6.0.7",
"handlebars": "4.7.9",
"http-proxy-middleware": "3.0.5",
"inquirer": "12.11.1",
"jwt-decode": "4.0.0",
"lodash": "4.18.1",
"node-fetch": "3.3.2",
"node-watch": "0.7.4",
"open": "10.2.0",
"ora": "8.2.0",
"p-limit": "6.2.0"
},
"devDependencies": {
"prettier": "^2.6.2"
"prettier": "3.8.3",
"vitest": "2.1.9"
},
"repository": {
"type": "git",
Expand Down
4 changes: 4 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export async function cli() {
.description(
'Synchronise (push) a remote Raisely campaign with the files on this machine'
)
.option(
'--no-validate',
'Skip pre-flight SCSS/component validation before upload'
)
.option(
'-f, --force',
'Deploy without asking for confirmation (non-interactive)'
Expand Down
176 changes: 154 additions & 22 deletions src/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,132 @@ import {
import { uploadPage } from './actions/pages.js';
import { loadConfig } from './config.js';
import { getToken } from './actions/auth.js';
import {
validateCampaignSass,
validateComponent,
} from './actions/validate.js';

function formatValidationErrors(errors) {
return errors.map(({ context, error }) => `${context}: ${error}`);
}

function normalizeValidationResult(result, fallbackError) {
if (result && typeof result === 'object' && typeof result.ok === 'boolean') {
return {
ok: result.ok,
error:
Comment thread
KeinerM marked this conversation as resolved.
typeof result.error === 'string' && result.error.trim()
? result.error
: fallbackError,
};
}

return {
ok: false,
error: fallbackError,
};
}

function toErrorMessage(error, fallback) {
if (typeof error === 'string' && error.trim()) return error.trim();
if (error instanceof Error && error.message.trim()) return error.message.trim();
if (error && typeof error.message === 'string' && error.message.trim()) {
return error.message.trim();
}
return fallback;
}

function getComponentNames() {
const componentsDir = path.join(process.cwd(), 'components');
if (!fs.existsSync(componentsDir)) {
return [];
}

return fs
.readdirSync(componentsDir, { withFileTypes: true })
.filter((entry) => entry.isDirectory())
.map((entry) => entry.name);
}

async function runPreflightValidation({ config }) {
const validationLimit = pLimit(4);
const loader = ora('Validating campaigns and components').start();
let campaigns = [];
try {
campaigns = await Promise.all(
config.campaigns.map(async (campaignUuid) => {
const campaign = await getCampaign({ uuid: campaignUuid });
return {
uuid: campaign.data.uuid,
path: campaign.data.path,
};
})
);
} catch (error) {
loader.fail('Deploy validation failed');
log(
`Campaign lookup failed: ${toErrorMessage(
error,
'Could not load campaign metadata for validation.'
)}`,
'red'
);
return false;
}
const componentNames = getComponentNames();

const validationTasks = [
...campaigns.map((campaign) =>
validationLimit(async () => {
const rawResult = await validateCampaignSass({
campaign,
token: config.token,
});
const result = normalizeValidationResult(
rawResult,
'SASS validator returned an invalid response.'
);
return {
ok: result.ok,
context: `Campaign ${campaign.path}`,
error: result.error,
};
})
),
...componentNames.map((name) =>
validationLimit(async () => {
const rawResult = await validateComponent({ name });
const result = normalizeValidationResult(
rawResult,
'Component validator returned an invalid response.'
);
return {
ok: result.ok,
context: `Component ${name}`,
error: result.error,
};
})
),
];
Comment thread
KeinerM marked this conversation as resolved.

Comment thread
cursor[bot] marked this conversation as resolved.
const results = await Promise.all(validationTasks);
const failed = results.filter((result) => !result.ok);

if (failed.length > 0) {
loader.fail('Deploy validation failed');
formatValidationErrors(failed).forEach((message) => {
log(message, 'red');
});
return false;
}

loader.succeed('Validation passed');
return true;
}

export default async function deploy(options = {}) {
const layout = detectLayout(process.cwd());
const cwd = process.cwd();
const layout = detectLayout(cwd);
if (shouldRefuseLayoutForCommand('deploy', layout)) {
br();
log(getLegacyLayoutRefusalMessage('deploy', layout), 'red');
Expand All @@ -33,12 +156,12 @@ export default async function deploy(options = {}) {

// load config
let config = await loadConfig();
await getToken(program, config);
config.token = await getToken(program, config);

welcome();
log(`You are about to deploy your local directly to Raisely`, 'white');
br();
console.log(` ${chalk.inverse(`${process.cwd()}`)}`);
console.log(` ${chalk.inverse(`${cwd}`)}`);
br();
if (config.apiUrl) {
br();
Expand Down Expand Up @@ -66,6 +189,17 @@ export default async function deploy(options = {}) {
}
}

if (options.validate !== false) {
const validationPassed = await runPreflightValidation({ config });
if (!validationPassed) {
br();
process.exitCode = 1;
return;
}
} else {
log('Skipping validation due to --no-validate flag.', 'yellow');
}

// upload campaign stylesheets
for (const campaignUuid of config.campaigns) {
const loader = ora(`Uploading styles for ${campaignUuid}`).start();
Expand All @@ -75,9 +209,11 @@ export default async function deploy(options = {}) {
try {
await uploadStyles(campaign.data.path);
} catch (e) {
loader.fail(`Failed to upload styles for ${campaignUuid}`);
br();
console.error(e);
process.exit(1);
process.exitCode = 1;
return;
Comment thread
cursor[bot] marked this conversation as resolved.
}

loader.succeed();
Expand All @@ -87,23 +223,19 @@ export default async function deploy(options = {}) {
const limit = pLimit(5);
const components = [];

const componentsDir = path.join(process.cwd(), 'components');
for (const file of fs.readdirSync(componentsDir)) {
const data = {
file: fs.readFileSync(
path.join(componentsDir, file, `${file}.js`),
'utf8'
),
config: JSON.parse(
fs.readFileSync(
path.join(componentsDir, file, `${file}.json`),
'utf8'
)
),
};
const componentsDir = path.join(cwd, 'components');
if (fs.existsSync(componentsDir)) {
for (const file of fs.readdirSync(componentsDir)) {
const data = {
file: fs.readFileSync(path.join(componentsDir, file, `${file}.js`), 'utf8'),
config: JSON.parse(
fs.readFileSync(path.join(componentsDir, file, `${file}.json`), 'utf8')
),
};

components.push(limit(() => updateComponentConfig(data)));
components.push(limit(() => updateComponentFile(data)));
components.push(limit(() => updateComponentConfig(data)));
components.push(limit(() => updateComponentFile(data)));
}
}

// Start loading all the components
Expand All @@ -125,12 +257,12 @@ export default async function deploy(options = {}) {

// upload pages
const pageFiles = await glob('campaigns/*/pages/**/*.json', {
cwd: process.cwd(),
cwd,
});

const pageTasks = [];
for (const file of pageFiles) {
const fullPath = path.join(process.cwd(), file);
const fullPath = path.join(cwd, file);
const pageData = JSON.parse(fs.readFileSync(fullPath, 'utf8'));
if (!pageData.uuid) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion tests/campaigns.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, describe } from 'node:test';
import { describe, test } from 'vitest';
import assert from 'node:assert/strict';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
Expand Down
2 changes: 1 addition & 1 deletion tests/command-layout-guard.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test } from 'node:test';
import { test } from 'vitest';
import assert from 'node:assert/strict';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
Expand Down
Loading