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

Put cdn.putDir logic inside OC #1321

Open
wants to merge 2 commits into
base: master
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@types/morgan": "1.9.3",
"@types/multer": "1.4.7",
"@types/node": "18.11.16",
"@types/node-dir": "^0.0.34",
"@types/parse-author": "2.0.1",
"@types/read": "0.0.29",
"@types/response-time": "2.3.5",
Expand Down Expand Up @@ -103,6 +104,7 @@
"morgan": "1.10.0",
"multer": "1.4.3",
"nice-cache": "0.0.5",
"node-dir": "^0.1.17",
"oc-client": "4.0.1",
"oc-client-browser": "1.5.9",
"oc-empty-response-handler": "1.0.2",
Expand Down
38 changes: 37 additions & 1 deletion src/registry/domain/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import fs from 'fs-extra';
import getUnixUtcTimestamp from 'oc-get-unix-utc-timestamp';
import path from 'path';
import dotenv from 'dotenv';
import { promisify } from 'util';
import nodeDir, { PathsResult } from 'node-dir';

import ComponentsCache from './components-cache';
import getComponentsDetails from './components-details';
Expand All @@ -23,6 +25,9 @@ import { StorageAdapter } from 'oc-storage-adapters-utils';
const packageInfo = fs.readJsonSync(
path.join(__dirname, '..', '..', '..', 'package.json')
);
const getPaths: (path: string) => Promise<PathsResult> = promisify(
nodeDir.paths
);

export default function repository(conf: Config) {
const cdn: StorageAdapter =
Expand Down Expand Up @@ -126,6 +131,37 @@ export default function repository(conf: Config) {
}
};

const putDir = async (dirInput: string, dirOutput: string) => {
const paths = await getPaths(dirInput);
const packageJsonFile = path.join(dirInput, 'package.json');
const files = paths.files.filter(file => file !== packageJsonFile);

const filesResults = await Promise.all(
files.map((file: string) => {
const relativeFile = file.slice(dirInput.length);
const url = (dirOutput + relativeFile).replace(/\\/g, '/');

const serverPattern = /(\\|\/)server\.js/;
const dotFilePattern = /(\\|\/)\..+/;
const privateFilePatterns = [serverPattern, dotFilePattern];
return cdn.putFile(
file,
url,
privateFilePatterns.some(r => r.test(relativeFile))
);
})
);
// Ensuring package.json is uploaded last so we can verify that a component
// was properly uploaded by checking if package.json exists
const packageJsonFileResult = await cdn.putFile(
packageJsonFile,
`${dirOutput}/package.json`.replace(/\\/g, '/'),
false
);

return [...filesResults, packageJsonFileResult];
};

const repository = {
getCompiledView(
componentName: string,
Expand Down Expand Up @@ -374,7 +410,7 @@ export default function repository(conf: Config) {
pkgDetails.packageJson
);

await cdn.putDir(
await putDir(
pkgDetails.outputFolder,
`${options!.componentsDir}/${componentName}/${componentVersion}`
);
Expand Down
1 change: 0 additions & 1 deletion src/registry/domain/storage-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ function convertLegacyAdapter(adapter: LegacyStorageAdapter): StorageAdapter {
getFile: fromCallback(adapter.getFile as any),
getJson: fromCallback(adapter.getJson as any),
listSubDirectories: fromCallback(adapter.listSubDirectories as any),
putDir: fromCallback(adapter.putDir as any),
putFile: fromCallback(adapter.putFile as any),
putFileContent: fromCallback(adapter.putFileContent as any),
getUrl: adapter.getUrl,
Expand Down
48 changes: 42 additions & 6 deletions test/unit/registry-domain-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,28 @@ describe('registry : domain : repository', () => {
listSubDirectories: sinon.stub().resolves(),
putFile: sinon.stub().resolves(),
getJson: sinon.stub().resolves(),
putDir: sinon.stub().resolves(),
putFileContent: sinon.stub().resolves(),
adapterType: 's3'
};

const nodeDirMock = {
paths(pathToDir, cb) {
cb(null, {
files: [
`${pathToDir}/package.json`,
`${pathToDir}/server.js`,
`${pathToDir}/.env`,
`${pathToDir}/template.js`
]
});
}
};

const Repository = injectr(
'../../dist/registry/domain/repository.js',
{
'fs-extra': fsMock,
'node-dir': nodeDirMock,
'./components-cache': () => componentsCacheMock,
'./components-details': () => componentsDetailsMock
},
Expand Down Expand Up @@ -378,8 +391,8 @@ describe('registry : domain : repository', () => {
componentsDetailsMock.refresh.resolves(
componentsDetailsBaseResponse
);
s3Mock.putDir = sinon.stub();
s3Mock.putDir.resolves('done');
s3Mock.putFile = sinon.stub();
s3Mock.putFile.resolves('done');
savePromiseResult(
repository.publishComponent(
{
Expand All @@ -406,10 +419,33 @@ describe('registry : domain : repository', () => {
});

it('should store the component in the correct directory', () => {
expect(s3Mock.putDir.args[0][0]).to.equal('/path/to/component');
expect(s3Mock.putDir.args[0][1]).to.equal(
'components/hello-world/1.0.1'
expect(s3Mock.putFile.args[0][0]).to.equal(
'/path/to/component/server.js'
);
expect(s3Mock.putFile.args[0][1]).to.equal(
'components/hello-world/1.0.1/server.js'
);
});

it('should store package.json as the last file', () => {
expect(s3Mock.putFile.args[3][1]).to.equal(
'components/hello-world/1.0.1/package.json'
);
});

it('should store server.js and dotfiles as private and the rest public', () => {
const getVisibility = name => {
const file = s3Mock.putFile.args.find(x => x[0].endsWith(name));

if (!file) return 'unknown';

return file[2] ? 'private' : 'public';
};

expect(getVisibility('server.js')).to.equal('private');
expect(getVisibility('.env')).to.equal('private');
expect(getVisibility('template.js')).to.equal('public');
expect(getVisibility('package.json')).to.equal('public');
});
});
});
Expand Down
1 change: 0 additions & 1 deletion test/unit/registry-domain-storage-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ describe('registry : domain : adapter', () => {
expect(parsed.getFile).to.be.equal('promisified');
expect(parsed.getJson).to.be.equal('promisified');
expect(parsed.listSubDirectories).to.be.equal('promisified');
expect(parsed.putDir).to.be.equal('promisified');
expect(parsed.putFile).to.be.equal('promisified');
expect(parsed.putFileContent).to.be.equal('promisified');
});
Expand Down
Loading