Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve decidim installation path from Gemfile.lock #1

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 7 additions & 14 deletions bin/decidimpack.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
#!/usr/bin/env node

const { exec, execSync } = require("child_process");
const createBuilds = require("../src/create-builds");
const resolveSource = require("../src/resolve-source");
const path = require("path");

const projectPath = process.argv[2];
let projectPath = process.argv[2];
if (!projectPath) {
throw new Error("Please provide the project path as the first argument!");
}

const resolveSource = () => {
return new Promise((resolve, _reject) => {
exec("bundle show decidim", { stdio: "pipe" }, (error, stdout) => {
if (error) {
resolve("https://github.com/decidim/decidim/packages#develop");
} else {
resolve(`${stdout.trim()}/packages`);
}
});
});
}
// Normalize the path to remove any relative references
projectPath = path.resolve(projectPath);

const buildDir = `${projectPath}/tmp/npmbuild`;

resolveSource().then((source) => {
resolveSource(projectPath).then((source) => {
console.log("Creating Decidim NPM builds...");
console.log(` source: ${source}`);
createBuilds(source, buildDir).then((packages) => {
console.log("Builds created:");
packages.forEach((pkg) => {
Expand Down
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"decidimpack": "bin/decidimpack.js"
},
"scripts": {
"install": "npx node bin/decidimpack.js $INIT_CWD"
"install": "bin/decidimpack.js $INIT_CWD"
},
"main": "index.js",
"dependencies": {
"@snyk/gemfile": "github:ahukkanen/gemfile#fix/current-path-parent-path",
"degit": "^2.8.4",
"fs-extra": "^10.0.0"
}
Expand Down
64 changes: 64 additions & 0 deletions src/resolve-source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const { exec } = require("child_process");
const { parse: parseGemfile } = require("@snyk/gemfile");
const path = require("path");

const resolveBundleSource = (projectPath) => {
return new Promise((resolve, reject) => {
exec("bundle show decidim", { stdio: "pipe", cwd: projectPath }, (error, stdout) => {
if (error) {
reject();
} else {
resolve(`${stdout.trim()}/packages`);
}
});
});
};

const resolveGemfileSource = (projectPath) => {
return new Promise((resolve, reject) => {
parseGemfile(`${projectPath}/Gemfile.lock`).then((gemfile) => {
for (const [type, defs] of Object.entries(gemfile)) {
if (type.match(/^GIT[0-9]*/)) {
if (defs.specs["decidim"]) {
resolve(`${defs.remote.path}/packages#${defs.revision.sha}`);
return;
}
} else if (type.match(/^PATH[0-9]*/)) {
if (defs.specs["decidim"]) {
let gemPath = defs.remote.path;
if (!gemPath) {
continue;
}
if (!gemPath.match(/^\//)) {
gemPath = path.resolve(projectPath, gemPath);
}
if (gemPath) {
resolve(`${gemPath}/packages`);
return;
}
}
}
}
}, reject);
});
};

module.exports = (projectPath) => {
return new Promise((resolve, _reject) => {
const tryList = [resolveBundleSource, resolveGemfileSource];
const tryNext = () => {
const current = tryList.shift();
if (current) {
current(projectPath).then(
(source) => resolve(source),
() => tryNext()
);
} else {
// Default to Decidim git repository
resolve("https://github.com/decidim/decidim/packages#develop");
}
};

tryNext();
});
};