Merge pull request #20980 from overleaf/jpa-extend-create-project

[web] scripts/create_project: fix and extend

GitOrigin-RevId: 4df41d02138a2214c83e7237794995d534e3c603
This commit is contained in:
Jakob Ackermann 2024-10-10 12:20:04 +02:00 committed by Copybot
parent 7d53b3e4a8
commit 7f6c2afc92

View file

@ -15,8 +15,8 @@ const ProjectEntityHandler = require('../app/src/Features/Project/ProjectEntityH
const EditorController = require('../app/src/Features/Editor/EditorController') const EditorController = require('../app/src/Features/Editor/EditorController')
const argv = parseArgs(process.argv.slice(2), { const argv = parseArgs(process.argv.slice(2), {
string: ['user-id', 'name'], string: ['user-id', 'name', 'random-operations', 'extend-project-id'],
boolean: ['old-history', 'random-content'], boolean: ['random-content'],
unknown: function (arg) { unknown: function (arg) {
console.error('unrecognised argument', arg) console.error('unrecognised argument', arg)
process.exit(1) process.exit(1)
@ -27,8 +27,11 @@ console.log('argv', argv)
const userId = argv['user-id'] const userId = argv['user-id']
const projectName = argv.name || `Test Project ${new Date().toISOString()}` const projectName = argv.name || `Test Project ${new Date().toISOString()}`
const oldHistory = argv['old-history'] let randomOperations = 0
const randomContent = argv['random-content'] if (argv['random-content'] === true || argv['random-operations']) {
randomOperations = parseInt(argv['random-operations'] || '1000', 10)
}
const extendProjectId = argv['extend-project-id']
console.log('userId', userId) console.log('userId', userId)
@ -40,7 +43,7 @@ async function _createRootDoc(project, ownerId, docLines) {
'main.tex', 'main.tex',
docLines, docLines,
ownerId, ownerId,
null 'create-project-script'
) )
await ProjectEntityUpdateHandler.promises.setRootDoc(project._id, doc._id) await ProjectEntityUpdateHandler.promises.setRootDoc(project._id, doc._id)
} catch (error) { } catch (error) {
@ -67,7 +70,7 @@ async function _addDefaultExampleProjectFiles(ownerId, projectName, project) {
'sample.bib', 'sample.bib',
bibDocLines, bibDocLines,
ownerId, ownerId,
null 'create-project-script'
) )
const frogPath = path.join( const frogPath = path.join(
@ -81,7 +84,7 @@ async function _addDefaultExampleProjectFiles(ownerId, projectName, project) {
frogPath, frogPath,
null, null,
ownerId, ownerId,
null 'create-project-script'
) )
} }
@ -107,8 +110,8 @@ async function _buildTemplate(templateName, userId, projectName) {
// Unfortunately we cannot easily change the timestamps of the history entries, so everything // Unfortunately we cannot easily change the timestamps of the history entries, so everything
// will be created at the same time. // will be created at the same time.
async function _pickRandomDoc(project) { async function _pickRandomDoc(projectId) {
const result = await ProjectEntityHandler.promises.getAllDocs(project._id) const result = await ProjectEntityHandler.promises.getAllDocs(projectId)
const keys = Object.keys(result) const keys = Object.keys(result)
if (keys.length === 0) { if (keys.length === 0) {
return null return null
@ -124,12 +127,12 @@ function nextId() {
return ('000000' + COUNTER++).slice(-6) return ('000000' + COUNTER++).slice(-6)
} }
async function _applyRandomDocUpdate(ownerId, project) { async function _applyRandomDocUpdate(ownerId, projectId) {
const action = _.sample(['create', 'edit', 'delete', 'rename']) const action = _.sample(['create', 'edit', 'delete', 'rename'])
switch (action) { switch (action) {
case 'create': // create a new doc case 'create': // create a new doc
await EditorController.promises.upsertDocWithPath( await EditorController.promises.upsertDocWithPath(
project._id, projectId,
`subdir/new-doc-${nextId()}.tex`, `subdir/new-doc-${nextId()}.tex`,
[`This is a new doc ${new Date().toISOString()}`], [`This is a new doc ${new Date().toISOString()}`],
'create-project-script', 'create-project-script',
@ -138,7 +141,7 @@ async function _applyRandomDocUpdate(ownerId, project) {
break break
case 'edit': { case 'edit': {
// edit an existing doc // edit an existing doc
const doc = await _pickRandomDoc(project) const doc = await _pickRandomDoc(projectId)
if (!doc) { if (!doc) {
return return
} }
@ -156,7 +159,7 @@ async function _applyRandomDocUpdate(ownerId, project) {
} }
lines[index] = thisLine lines[index] = thisLine
await EditorController.promises.upsertDocWithPath( await EditorController.promises.upsertDocWithPath(
project._id, projectId,
doc.path, doc.path,
lines, lines,
'create-project-script', 'create-project-script',
@ -166,28 +169,28 @@ async function _applyRandomDocUpdate(ownerId, project) {
} }
case 'delete': { case 'delete': {
// delete an existing doc (but not the root doc) // delete an existing doc (but not the root doc)
const doc = await _pickRandomDoc(project) const doc = await _pickRandomDoc(projectId)
if (!doc || doc.path === '/main.tex') { if (!doc || doc.path === '/main.tex') {
return return
} }
await EditorController.promises.deleteEntityWithPath( await EditorController.promises.deleteEntityWithPath(
project._id, projectId,
doc.path, doc.path,
ownerId, 'create-project-script',
'create-project-script' ownerId
) )
break break
} }
case 'rename': { case 'rename': {
// rename an existing doc (but not the root doc) // rename an existing doc (but not the root doc)
const doc = await _pickRandomDoc(project) const doc = await _pickRandomDoc(projectId)
if (!doc || doc.path === '/main.tex') { if (!doc || doc.path === '/main.tex') {
return return
} }
const newName = `renamed-${nextId()}.tex` const newName = `renamed-${nextId()}.tex`
await EditorController.promises.renameEntity( await EditorController.promises.renameEntity(
project._id, projectId,
doc._id, doc._id,
'doc', 'doc',
newName, newName,
@ -204,25 +207,28 @@ async function createProject() {
const user = await User.findById(userId) const user = await User.findById(userId)
console.log('Will create project') console.log('Will create project')
console.log('user_id:', userId, '=>', user.email) console.log('user_id:', userId, '=>', user.email)
console.log('project name:', projectName) let projectId
const attributes = oldHistory ? { overleaf: {} } : {} if (extendProjectId) {
const project = await ProjectCreationHandler.promises.createBlankProject( console.log('extending existing project', extendProjectId)
userId, projectId = extendProjectId
projectName, } else {
attributes console.log('project name:', projectName)
) const project = await ProjectCreationHandler.promises.createBlankProject(
await _addDefaultExampleProjectFiles(userId, projectName, project) userId,
if (randomContent) { projectName
for (let i = 0; i < 1000; i++) { )
await _applyRandomDocUpdate(userId, project) await _addDefaultExampleProjectFiles(userId, projectName, project)
} projectId = project._id
} }
return project for (let i = 0; i < randomOperations; i++) {
await _applyRandomDocUpdate(userId, projectId)
}
return projectId
} }
createProject() createProject()
.then(project => { .then(projectId => {
console.log('Created project', project._id) console.log('Created project', projectId)
process.exit() process.exit()
}) })
.catch(err => { .catch(err => {