Skip to content

Commit

Permalink
Add missing yarn and stop use artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
haimkastner committed Sep 9, 2023
1 parent 53d2d4e commit e27b7c6
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 167 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
REACT_APP_API_URL="http://127.0.0.1"
REACT_APP_MOCK_API_URL="http://127.0.0.1:8080"
REACT_APP_V3_URL="http://127.0.0.1:8081"
REACT_APP_LIGHTWEIGHT_URL"/light-app/index.html"
REACT_APP_LIGHTWEIGHT_URL="/light-app/index.html"
REACT_APP_LOCAL_DEV="true"
REACT_APP_MOCK_MODE=""
REACT_APP_SHOW_VERSION_COMMIT=true
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@
"cordova-plugin-whitelist": "^1.3.5",
"http-server": "^14.1.0",
"jest": "^28.1.3",
"jszip": "^3.7.1",
"node-fetch": "^3.0.0",
"puppeteer": "^13.1.1",
"react-scripts": "4.0.3",
Expand Down
55 changes: 37 additions & 18 deletions scripts/fetch-light-app.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,55 @@
import fse from 'fs-extra';
import path from 'path';
import jsZip from 'jszip';
import nodeFetch from 'node-fetch';
import util from 'util';
import { exec } from 'child_process';
import dotenv from 'dotenv';
const execPromise = util.promisify(exec);

dotenv.config();

// Pull latest git, dont use artifacts at all, see run-mock server
const BUILD_MODE = process.env.BUILD_PATH === 'internal' ? 'internal' : 'www';
const dashboardDist = path.join(BUILD_MODE, 'light-app');

const ENV_BRANCH = (process.env.BRANCH !== 'main' && !process.env.BUILD_PROD) ? 'develop' : 'main';

console.log(`[fetch-light-app] Fetching light-app for branch "${process.env.BRANCH}" from server "${ENV_BRANCH}" branch...`);

async function downloadAndUnpackDashboard(dashboardArtifact, distDir) {
const latestArtifact = await nodeFetch(dashboardArtifact);
const artifactBuffer = await latestArtifact.buffer();

const artifactZip = await jsZip.loadAsync(artifactBuffer);

for (const [filename, file] of Object.entries(artifactZip.files)) {
if (file.dir) {
continue;
}
function copyDirectorySync(source, destination) {
// Create the destination directory if it doesn't exist
if (!fse.existsSync(destination)) {
fse.mkdirSync(destination);
}

const fileBuffer = await file.async('nodebuffer');
// Read the contents of the source directory
const files = fse.readdirSync(source);

for (const file of files) {
const sourceFilePath = path.join(source, file);
const destinationFilePath = path.join(destination, file);

const fileDist = path.join(distDir, filename);
await fse.promises.mkdir(path.dirname(fileDist), { recursive: true });
fse.outputFileSync(fileDist, fileBuffer);
// Check if the current item is a directory
if (fse.statSync(sourceFilePath).isDirectory()) {
// If it's a directory, recursively copy its contents
copyDirectorySync(sourceFilePath, destinationFilePath);
} else {
// If it's a file, copy it to the destination directory
fse.copyFileSync(sourceFilePath, destinationFilePath);
}
}
}

(async () => {
// Download the dashboard app
await downloadAndUnpackDashboard(`https://nightly.link/casanet/lightweight-dashboard/workflows/build/${ENV_BRANCH}/${BUILD_MODE}.zip`, dashboardDist);

try { await execPromise(`mkdir ${path.join('temp-mock')}`); } catch {}
try { await execPromise(`cd ${path.join('temp-mock')} && git clone https://github.com/casanet/lightweight-dashboard.git && git checkout ${ENV_BRANCH}`); } catch {}
await execPromise(`cd ${path.join('temp-mock', 'lightweight-dashboard')} && git checkout ${ENV_BRANCH} && git pull`);
try {
const setEnvRes = await execPromise(`cd ${path.join('temp-mock', 'lightweight-dashboard')} && node scripts/set-environment.js`, { env: { API_URL: process.env.REACT_APP_API_URL } });
console.log(setEnvRes.stdout);
} catch (error) {
console.log(error);
}

copyDirectorySync(path.join('temp-mock', 'lightweight-dashboard', 'src'), dashboardDist);
})();
46 changes: 3 additions & 43 deletions scripts/fetch-spec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import fse from 'fs-extra';
import path from 'path';
import jsZip from 'jszip';
import nodeFetch from 'node-fetch';
import dotenv from 'dotenv';
import nodeFetch from 'node-fetch';

dotenv.config();

// The API owner
const API_SPEC_OWNER = 'casanet';
// The API name
const API_SPEC_NAME = 'casanet-server';

// The branch to take spec from, as default use main branch
const API_SERVER_SPEC_BRANCH = process.env.API_SERVER_SPEC_BRANCH !== 'main' ? 'development' : 'master';

Expand Down Expand Up @@ -39,38 +33,6 @@ const AUTO_GEN_COMMENT = `\n// This file was AutoGenerated at ${new Date()}'\n\n
fse.appendFileSync(path.join(sourceDestination), sharedModelsResponseBuffer);
}

async function downloadSpec() {

const finalURL = `https://nightly.link/${API_SPEC_OWNER}/${API_SPEC_NAME}/workflows/nodejs/${API_SERVER_SPEC_BRANCH}/swagger-spec.zip`;
console.log(`[fetch-api] Fetching API spec form artifactory "${finalURL}"...`);

// Download the swagger API spec from the API server CI latest artifact https://github.com/haimkastner/node-api-spec-boilerplate/actions/workflows/actions.yml
// Using https://nightly.link/ for download latest build dist
const latestArtifact = await nodeFetch(finalURL);

// Get res buffer data
const artifactBuffer = await latestArtifact.arrayBuffer();

// Load buffer as a zip archive
const artifactZip = await jsZip.loadAsync(artifactBuffer);

// Fetch the archived spec file
const archivedSpecFile = artifactZip.file(SPEC_FILE_NAME);

// Extract file content as buffer
const fileBuffer = await archivedSpecFile.async('nodebuffer');

// Build the file full path
const fileDist = path.join(SPEC_FILE_DEST_DIR, SPEC_FILE_NAME);

console.log(`[fetch-api] Saving API Spec to "${fileDist}"`);

// Create generated dir if not yet exists
await fse.promises.mkdir(path.dirname(fileDist), { recursive: true });
// Save the fetched spec into it
fse.outputFileSync(fileDist, fileBuffer);
}

(async () => {

// If local path has been set, use it
Expand All @@ -86,10 +48,8 @@ async function downloadSpec() {
}

console.log(`[fetch-api] Fetching API Spec form server "${API_SERVER_SPEC_BRANCH}" branch...`);
await downloadSpec();
console.log(`[fetch-api] API Spec fetched successfully`);

// Download the latest channel TS spec API
await downloadSourceFile(`https://raw.githubusercontent.com/casanet/casanet-server/${API_SERVER_SPEC_BRANCH}/backend/src/models/remote2localProtocol.ts`, CHANNEL_SPEC_PATH);
await downloadSourceFile(`https://raw.githubusercontent.com/casanet/casanet-server/${API_SERVER_SPEC_BRANCH}/backend/src/models/sharedInterfaces.d.ts`, SHARED_MODELS_PATH);
await downloadSourceFile(`https://raw.githubusercontent.com/casanet/casanet-server/${API_SERVER_SPEC_BRANCH}/backend/src/generated/swagger.json`, path.join(SPEC_FILE_DEST_DIR, SPEC_FILE_NAME));
console.log(`[fetch-api] API Spec fetched successfully`);
})();
Loading

0 comments on commit e27b7c6

Please sign in to comment.