Skip to content

Commit

Permalink
Merge pull request #27 from prismicio/alw/themes
Browse files Browse the repository at this point in the history
allow content to be inserted while creating a repository
  • Loading branch information
arnaudlewis authored Apr 23, 2019
2 parents 320261c + aae2d4c commit 0efc2dc
Show file tree
Hide file tree
Showing 2 changed files with 782 additions and 767 deletions.
72 changes: 53 additions & 19 deletions lib/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ import configuration from './config';

const isWin = /^win/.test(process.platform);

function queryCreateRepositoryWithCookie(base, domain, cookies, customTypes, users) {
function queryCreateRepositoryWithCookie(base, domain, cookies, customTypes, signedDocuments, users) {
const url = `${base}/authentication/newrepository`;
const data = {
domain,
plan: 'personal',
isAnnual: 'false',
};
if (customTypes) data['custom-types'] = JSON.stringify(customTypes);
if (signedDocuments) {
data.signature = signedDocuments.signature;
data.documents = JSON.stringify(signedDocuments.docs);
}
if (users) data.users = users;
return Communication.post(url, data, cookies);
}

function queryCreateRepositoryWithToken(base, domain, token, customTypes, users) {
function queryCreateRepositoryWithToken(base, domain, token, customTypes, signedDocuments, users) {
const matches = base.match(/(https?:\/\/)(.*)/);
const protocol = matches[1];
const plateform = matches[2];
Expand All @@ -34,20 +38,24 @@ function queryCreateRepositoryWithToken(base, domain, token, customTypes, users)
isAnnual: 'false',
};
if (customTypes) data['custom-types'] = JSON.stringify(customTypes);
if (signedDocuments) {
data.signature = signedDocuments.signature;
data.documents = JSON.stringify(signedDocuments.docs);
}
if (users) data.users = users;
return Communication.post(url, data);
}

async function createWithDomain(base, domain, args, customTypes, users, noconfirm) {
async function createWithDomain(base, domain, args, customTypes, signedDocuments, users, noconfirm) {
const oauthAccessToken = args['--oauthaccesstoken'];

let eventuallyCreateRepository;

if (oauthAccessToken) {
eventuallyCreateRepository = queryCreateRepositoryWithToken(base, domain, oauthAccessToken, customTypes, users);
eventuallyCreateRepository = queryCreateRepositoryWithToken(base, domain, oauthAccessToken, customTypes, signedDocuments, users);
} else {
const cookies = await Authentication.connect(base, args, noconfirm);
eventuallyCreateRepository = queryCreateRepositoryWithCookie(base, domain, cookies, customTypes, users);
eventuallyCreateRepository = queryCreateRepositoryWithCookie(base, domain, cookies, customTypes, signedDocuments, users);
}

return eventuallyCreateRepository.then(() => {
Expand All @@ -57,10 +65,10 @@ async function createWithDomain(base, domain, args, customTypes, users, noconfir
if (statusCode === 401) {
// remove cookie
return configuration.set({ cookies: '' }).then(() => (
createWithDomain(base, domain, args, customTypes, users, noconfirm)
createWithDomain(base, domain, args, customTypes, signedDocuments, users, noconfirm)
));
}
return Helpers.UI.displayErrors('An unexcepted error occured');
return Helpers.UI.displayErrors('An unexpected error occured');
});
}

Expand Down Expand Up @@ -209,6 +217,40 @@ function readCustomTypes(folder) {
return null;
}

function readDocuments(folder) {
const docNameFromFilename = (filename) => {
const matched = filename.match(/(.*)\.json/);
if (!matched) throw new Error(`Invalid document filename ${filename}`);
else return matched[1];
};

if (folder) {
const docsFolder = path.join(folder, 'documents');
const metaPath = path.join(docsFolder, 'index.json');
if (shell.test('-e', metaPath)) {
const { signature } = JSON.parse(shell.cat(metaPath));
if (!signature) throw new Error('Missing signature in your prismic documents dump.');
const langIds = shell.ls(docsFolder).filter(p => !p.match('index.json'));

const docs = langIds.reduce((docByLangAcc, langId) => {
const langPath = path.join(docsFolder, langId);
const docsFilename = shell.ls(langPath);
const docsForLang = docsFilename.reduce((docAcc, docFilename) => {
const docName = docNameFromFilename(docFilename);
const docValue = JSON.parse(shell.cat(path.join(langPath, docFilename)));

return Object.assign({}, docAcc, { [docName]: docValue });
}, {});

return Object.assign({}, docByLangAcc, docsForLang);
}, {});

return { signature, docs };
}
}
return null;
}

async function readZipAndCreateRepoWithCustomTypes(newRepository, base, domain, args, template, folder, theme, noconfirm) {
const tmpfolder = theme ? theme.tmpFolder : await Template.unzip(template.url, template.innerFolder);
const initTemplate = () => {
Expand All @@ -230,24 +272,16 @@ async function readZipAndCreateRepoWithCustomTypes(newRepository, base, domain,
// Create repository if needed
if (newRepository) {
const customTypes = readCustomTypes(tmpfolder);
const signedDocs = readDocuments(tmpfolder);
const users = args['--users'];
await createWithDomain(base, domain, args, customTypes, users, noconfirm);
await createWithDomain(base, domain, args, customTypes, signedDocs, users, noconfirm);
}
initTemplate();
return null;
}

function queryValidateCLI(base, domain) {
const baseWithDomain = Helpers.Domain.repository(base, domain);
const url = `${baseWithDomain}/app/settings/onboarding/cli`;
return Communication.post(url, {});
}

function installAndDisplayInstructions(base, domain, template, folder) {
function installAndDisplayInstructions(template, folder) {
if (folder) {
queryValidateCLI(base, domain).catch(() => {
// Required to prevent query error uncaught if onboarding not available in the writing room
});
Helpers.UI.display('Running npm install...');
const devnull = isWin ? 'NUL' : '/dev/null';
shell.cd(folder);
Expand All @@ -269,7 +303,7 @@ async function create(templates, base, domain, args, theme) {
const folder = await chooseFolder(d, args, noconfirm);
const template = theme ? theme.template : await chooseTemplate(d, templates, args, noconfirm);
await readZipAndCreateRepoWithCustomTypes(newRepository, base, d, args, template, folder, theme, noconfirm);
installAndDisplayInstructions(base, d, template, folder);
installAndDisplayInstructions(template, folder);
}

function isGithubRepository(value) {
Expand Down
Loading

0 comments on commit 0efc2dc

Please sign in to comment.