Skip to content

Commit

Permalink
accumulate readme and packageDetail on backend
Browse files Browse the repository at this point in the history
  • Loading branch information
dmstern committed Aug 19, 2018
1 parent 0826210 commit d6bc464
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 68 deletions.
97 changes: 49 additions & 48 deletions server/artifactory-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,65 +28,67 @@ async function fetchPackages() {
return (request = axios.get(`/-/all`));
}

async function getReadme({ scope, packageName }) {
if (process.env.MOCK) {
console.log('bla');
return new Promise((resolve, reject) => {
async function getPackageDetail({ scope, packageName }) {
const packageDetailResonse = process.env.MOCK ?
await new Promise((resolve, reject) => {
resolve({
data: require(`./mock/fractal-menu-enhancer.json`)
});
}) : await axios.get(`/${name2url({ scope, packageName })}`);

const readme = process.env.MOCK ?
await new Promise((resolve, reject) => {
const converter = new showdown.Converter();
const readme = fs.readFileSync("./server/mock/fractal-menu-enhancer.readme.md");
const html = converter.makeHtml(readme.toString());
resolve({
data: html
});
}) : await new Promise(async (resolve, reject) => {
const packageDetail = packageDetailResonse.data;
const latestVersionResponse = await getDistTags({ scope, packageName });
const latestVersion = latestVersionResponse.data.latest;
const downloadUrl = packageDetail.versions[latestVersion].dist.tarball;
axios
// Request package:
.request({ // TODO: check if readme has been already downloaded
responseType: "arraybuffer",
url: downloadUrl,
method: "get",
headers: {
"Content-Type": "application/gzip"
}
})
// Store package in filesystem:
.then(result => {
fs.ensureDirSync(`${tmpDir}/${scope}/${packageName}`);
const outputFilename = `${tmpDir}/${scope}/${packageName}/${packageName}-${latestVersion}.tar.gz`;
fs.writeFileSync(outputFilename, result.data);
return outputFilename;
})
// Extract package:
.then(file => {
const cwd = `${tmpDir}/${scope}/${packageName}`;
return tar.x({ file, cwd }).then(() => cwd);
})
// Read README.md:
.then(dir => {
const readmeFile = `${dir}/package/README.md`; // TODO: ignore case
const readme = fs.readFileSync(readmeFile);
const converter = new showdown.Converter();
const html = converter.makeHtml(readme.toString());
return html;
}).then((result) => {
resolve(result);
});
});
}

const packageDetailResonse = await getPackageDetail({ scope, packageName });
const packageDetail = packageDetailResonse.data;
const latestVersionResponse = await getDistTags({ scope, packageName });
const latestVersion = latestVersionResponse.data.latest;
const downloadUrl = packageDetail.versions[latestVersion].dist.tarball;
return axios
// Request package:
.request({ // TODO: check if readme has been already downloaded
responseType: "arraybuffer",
url: downloadUrl,
method: "get",
headers: {
"Content-Type": "application/gzip"
}
})
// Store package in filesystem:
.then(result => {
fs.ensureDirSync(`${tmpDir}/${scope}/${packageName}`);
const outputFilename = `${tmpDir}/${scope}/${packageName}/${packageName}-${latestVersion}.tar.gz`;
fs.writeFileSync(outputFilename, result.data);
return outputFilename;
})
// Extract package:
.then(file => {
const cwd = `${tmpDir}/${scope}/${packageName}`;
return tar.x({ file, cwd }).then(() => cwd);
})
// Read README.md:
.then(dir => {
const readmeFile = `${dir}/package/README.md`; // TODO: ignore case
const readme = fs.readFileSync(readmeFile);
const converter = new showdown.Converter();
const html = converter.makeHtml(readme.toString());
return html;
});
}

async function getPackageDetail({ scope, packageName }) {
if (process.env.MOCK) {
packageDetailResonse.data.readme = readme;
return new Promise((resolve, reject) => {
resolve({
data: require(`./mock/fractal-menu-enhancer.json`)
data: packageDetailResonse,
});
});
}
return axios.get(`/${name2url({ scope, packageName })}`);
}

async function getDistTags({ scope, packageName }) {
Expand All @@ -95,7 +97,6 @@ async function getDistTags({ scope, packageName }) {

module.exports = {
fetchPackages,
getReadme,
getDistTags,
getPackageDetail
};
16 changes: 8 additions & 8 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ app.get('/packages', function(req, res) {
artifactoryService.fetchPackages().then(response => {
res.send(response.data);
}).catch(error => {
console.log(error);
throw error;
});
});

Expand All @@ -33,12 +33,12 @@ app.get('/:scope?/:packageName', function(req, res) {
});
});

app.get('/:scope?/:packageName/readme', async function(req, res) {
artifactoryService.getReadme(req.params).then((response) => {
res.send(response);
}).catch(error => {
throw error;
});
});
// app.get('/:scope?/:packageName/readme', async function(req, res) {
// artifactoryService.getReadme(req.params).then((response) => {
// res.send(response);
// }).catch(error => {
// throw error;
// });
// });

app.listen(30001);
6 changes: 3 additions & 3 deletions src/services/BackendApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export default class BackendApi {
return this.get(`${scope ? `${scope}/` : ''}${packageName}`);
}

public getReadme({scope, packageName}): AxiosPromise<string> {
return this.get(`${scope ? `${scope}/` : ''}${packageName}/readme`);
}
// public getReadme({scope, packageName}): AxiosPromise<string> {
// return this.get(`${scope ? `${scope}/` : ''}${packageName}/readme`);
// }

private get(route: string): AxiosPromise<any> {
return this.axios.get(route);
Expand Down
11 changes: 3 additions & 8 deletions src/services/PackageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class PackagesService {
private packages!: Package[];
private packageNamesList!: string[];
private searchItemList!: SearchItem[];
private packageDetails!: { // TODO: move this caching to backend
private packageDetails!: {
[packageName: string]: Package,
};

Expand All @@ -46,13 +46,8 @@ export default class PackagesService {

return new Promise<Package|string>((fulfill, reject) => {
BackendApi.Instance.getPackageDetail({scope, packageName}).then((response) => {
return this.packageDetails[key] = response.data;
}).then((packageDetails) => {
return BackendApi.Instance.getReadme({scope, packageName}).then((response) => {
return this.packageDetails[key].readme = response.data;
});
}).then(() => {
fulfill(this.packageDetails[key]);
this.packageDetails[key] = response.data;
return fulfill(this.packageDetails[key]);
});
});
}
Expand Down
1 change: 0 additions & 1 deletion src/views/PackageDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export default class PackageDetail extends Vue {
scope: Router.currentRoute.params.scope,
packageName: Router.currentRoute.params.packageName,
}).then((response) => {
console.log('response', response);
this.data.packageDetail = response;
});
}
Expand Down

0 comments on commit d6bc464

Please sign in to comment.